Angularjs 角度2-组件如何识别URL更改?

Angularjs 角度2-组件如何识别URL更改?,angularjs,angular,angular2-routing,Angularjs,Angular,Angular2 Routing,使用Angular 2,我有一个基于URL哈希订阅服务的组件。URL更改不会调用任何子组件。我认为这需要路线,尽管位置可能更合适。如何侦听pop状态事件并作出反应?如果您希望侦听路由更改,可以使用路由器 import {Location} from '@angular/common'; import {Router} from '@angular/router' export class SomeComponent { constructor(router:Router, private

使用Angular 2,我有一个基于URL哈希订阅服务的组件。URL更改不会调用任何子组件。我认为这需要路线,尽管位置可能更合适。如何侦听pop状态事件并作出反应?

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

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

这就是您想要做的吗?

您在哪里找到Router.changes()?它没有列在列表中。还显示“
changes:Observable
路由器的可观察或url更改。”@GünterZöchbauer当我运行上述代码时,我得到一个错误。代码编制是否正确?我正在运行版本2.0.0-beta.9
TypeError:router.changes未定义
给定的示例是angular2-rc1,它类似于
router.subscribe(结果=>this.routeChanged(结果));private routeChanged(path:string):void{console.log(“New Route:+path”);}
您可以从方法参数获取当前路由,因为angular2-rc0我认为它已更改,并且您只获取void方法头。所以我在注射Location@Sebastian切换到仅使用
router.subscribe()
抛出
TypeError:指令为空
。也许解决这个问题的更好方法是解释如何向路由添加指令。你能推荐一个最新的教程吗?