【问题标题】:In angular 2 how do you detect route changes在角度 2 中,您如何检测路线变化
【发布时间】:2016-10-31 01:19:19
【问题描述】:

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

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

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

【问题讨论】:

    标签: angular


    【解决方案1】:

    在新路由器中

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

    另见Angular 2 router event listener

    【讨论】:

    猜你喜欢
    • 2018-05-07
    • 2018-06-23
    • 1970-01-01
    • 2021-06-17
    • 2017-03-12
    • 2017-09-01
    • 2021-08-07
    • 2018-10-24
    相关资源
    最近更新 更多