2016-10-30 24 views
5

To jest moja obecna próba użycia starych bibliotek.W kanciastym 2, w jaki sposób można wykryć zmiany trasy

jestem teraz przy użyciu najnowszych bibliotekami, w jaki sposób mogę zaktualizować to:

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();   
     }) 
    } 
} 

To mój moduł:

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 { } 

Odpowiedz

12

W nowym routerem to

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

Zobacz także Angular 2 router event listener

+0

To wydaje się działać dobrze. this.location.path() nie działa z nowymi bibliotekami, żadnymi pomysłami? jego w powyższym kodzie i narzekać na ścieżkę – AngularM

+0

'this.location.path()' powinien działać https://angular.io/docs/ts/latest/api/common/index/Location-class.html –

+1

to działa w porządku, miałem to poza zakresem – AngularM

Powiązane problemy