2016-10-30 4 views
5

これは私の現在の古いライブラリの使用です。角度2ではどのように経路の変更を検出しますか

私は今、最新のLIBSを使用してい

は、どのように私はこれを更新してしまう:

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

@Component({ 
    moduleId: module.id, 
    selector: 'HeaderComponent', 
    templateUrl: 'header.component.html' 
}) 

export class HeaderComponent{ 
    router : Router; 

    constructor(router: Router, location: Location){ 
     this.router = router; 

     this.router.changes.subscribe((currentRoute) => { 
      let currentRoute = this.location.path();   
     }) 
    } 
} 

これは私のモジュールです:新しいルータでは

export * from './header.component'; 

import { NgModule } from '@angular/core'; 
import { CommonModule } from '@angular/common'; 
import { RouterModule } from '@angular/router'; 

import { HeaderComponent } from './header.component'; 

@NgModule({ 
    imports: [RouterModule, CommonModule], 
    declarations: [HeaderComponent], 
    exports: [HeaderComponent], 
    providers: [] 
}) 

export class HeaderModule { } 

答えて

12

それはだ

this.router.events.subscribe(...) 

も参照してください。 Angular 2 router event listener

+0

これはうまくいくようです。 this.location.path()は新しいlibsで動作しません。その私の上記のコードとそのパス – AngularM

+0

文句 'this.location.path()' https://angular.io/docs/ts/latest/api/common/index/Location-class.html –

+1

を動作するはずで、それは動作します私はそれを範囲外に持っていた – AngularM

関連する問題