Angular 2 - 组件如何识别URL更改?

时间:2016-05-12 21:35:03

标签: angularjs angular angular2-routing

使用Angular 2,我有一个基于URL哈希订阅服务的组件。 URL更改不会调用任何子组件。我认为这需要Route,尽管Location可能更合适。我如何倾听并回应流行音乐事件?

1 个答案:

答案 0 :(得分:2)

如果您希望收听路线更改,可以使用路由器。

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

这是你想做的吗?

相关问题