Get current route without parameters

前端 未结 12 1231
梦如初夏
梦如初夏 2021-01-01 09:08

I need to get my current route without params in Angular 2, I found a way to get the current route with params as follows:

 this.router.url

12条回答
  •  被撕碎了的回忆
    2021-01-01 09:16

    this could help you:

    1. Import Router:

      import { Router } from '@angular/router';
      
    2. Signature in the constructor:

      constructor(private _router: Router) {}
      
    3. Check the _router events property:

      this._router.events
          .subscribe(
              (url:any) => {
                  let _ruta = "";
                  url.url.split("/").forEach(element => {
                      if(element!=="" && _ruta==="")
                          _ruta="/"+element;
                      });
                  console.log("route: "+_ruta); //<<<---- Root path
                  console.log("to URL:"+url.url); //<<<---- Destination URL 
                  console.log("from URL:"+this._router.url);//<<<---- Current URL
              }); 
      

提交回复
热议问题