2016-05-12 18 views
1

Angular 2を使用すると、URLハッシュに基づいてサービスに加入するコンポーネントがあります。 URLの変更によってサブコンポーネントは呼び出されません。私はこれがルートを必要とすると仮定しますが、ロケーションがより適切かもしれません。ポップ州のイベントにどのように耳を傾けて反応しますか?角度2 - コンポーネントはURLの変更をどのように認識しますか?

答えて

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

これはあなたのやりたいことですか?

+0

ここで、Router.changes()は見つかりましたか? [ルーターのドキュメント](https://angular.io/docs/js/latest/api/router/Router-class.html)に記載されていません – ebakunin

+0

https://github.com/angular/angular/blob/b30ddfbfc5192f526ceaac525cf6965635831c46 /modules/%40angular/router/src/router.ts#L74。 https://angular.io/docs/ts/latest/api/router/index/Router-interface.htmlにも「変更点があります:Observable 」 ルーターからの観測またはURLの変更があります。 –

+0

@GünterZöchbauer上記のコードを実行すると、エラーが発生します。コードは正しく作成されていますか?私はバージョン2.0.0-beta.9を実行しています。 'TypeError:router.changes is undefined' – ebakunin

関連する問題