角度2ルータの状態変化を監視するにはどうすればよいですか?角2ルータイベントリスナー
私は、このイベントを使用角度1.xでは:
だから、$rootScope.$on('$stateChangeStart',
function(event,toState,toParams,fromState,fromParams, options){ ... })
、私は角2に、このイベントリスナーを使用する場合:
window.addEventListener("hashchange",() => {return console.log('ok')}, false);
それは 'OK' を返していない、その変更JSからの状態、ブラウザのhistory.back()関数が実行されます。サービスとして
使用router.subscribe()関数:ルーティングでのinit成分で
import {Injectable} from 'angular2/core';
import {Router} from 'angular2/router';
@Injectable()
export class SubscribeService {
constructor (private _router: Router) {
this._router.subscribe(val => {
console.info(val, '<-- subscribe func');
})
}
}
噴射サービス:
import {Component} from 'angular2/core';
import {Router} from 'angular2/router';
@Component({
selector: 'main',
templateUrl: '../templates/main.html',
providers: [SubscribeService]
})
export class MainComponent {
constructor (private subscribeService: SubscribeService) {}
}
私は、この例のような他の成分は、このサービスを注入。それから、状態が変わり、console.info()が動作しない状態になります。
私は間違っていますか?
[ gular 2?](https://stackoverflow.com/questions/33520043/how-to-detect-a-route-change-in-angular-2) –