Angular2 @angular/router 3.0.0-alpha.3 - How to get route name or path when route changes

前端 未结 2 1993
时光取名叫无心
时光取名叫无心 2021-02-10 07:15

I\'m trying to get the current route name or route path in my app.component when the route changes so that I can use the route name as a page class around a wrapper div. I\'m t

2条回答
  •  无人及你
    2021-02-10 08:05

    This would probably help,

    import { CanActivate, Router, RouterStateSnapshot } from '@angular/router';
    
    @Component({
      selector: 'my-component',
      templateUrl: 'my-component.html'
    })
    export class MyComponent implements CanActivate {
      constructor(private router: Router) {}
    
      canActivate(state: RouterStateSnapshot) {
       // this is the future route you are intended to visit
       console.log(state.url);
      }
    }
    

    More details at https://angular.io/docs/ts/latest/guide/router.html

提交回复
热议问题