在角度2中,您如何检测路线变化

时间:2016-10-30 17:17:57

标签: angular

这是我目前使用旧库的尝试。

我现在正在使用最新的库,我该如何更新:

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

1 个答案:

答案 0 :(得分:14)

在新路由器中

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

另见Angular 2 router event listener