2016-05-15 30 views
1

如何將某些參數傳遞給我的rotes,就像在舊路由器中一樣?Angular 2路由器傳遞參數並在變化時獲取它們

@RouteConfig([ 
    new Route({path: '/home', component: HomeCmp, name: 'HomeCmp' }) 
]) 

@Routes([ 
    {path: '/login', component: Component} 
]) 

,然後讓他們在認購的變化?

export class AppComponent { 

     public constructor(private router: Router) { 

      this.router.changes.subscribe((x) => { 
       console.log(x); 
      }); 

我的意思是在老角有一個狀態,我們可以將事件附加onBefore ECT並獲得當前的狀態。這實際上是如何工作的。 this.router.changes.subscribe - 它觸發了更改,但不提供任何有關它的信息 - x未定義。

回答

0

訂閱中不存在任何參數。您需要使用位置服務來檢查當前路線:

import {Location} from '@angular/common'; 
import {Router} from '@angular/router' 

export class SomeComponent { 
    constructor(router:Router, private location:Location) { 
    router.changes.subscribe(() => this.routeChanged()); 
    } 

    private routeChanged():void { 
    var path:string = this.location.path(); 
    console.log("Path:" + path); 
    } 
} 
相關問題