Jak odczytywać zmianę stanu w routerze Angular 2?Słyszalny odbiornik zdarzeń routera 2
kątowej 1.x Kiedyś to wydarzenie:
$rootScope.$on('$stateChangeStart',
function(event,toState,toParams,fromState,fromParams, options){ ... })
Więc, jeśli mogę użyć tego EventListener kątowej 2:
window.addEventListener("hashchange",() => {return console.log('ok')}, false);
to nie jest powrót 'OK', a następnie zmienić stan z JS, tylko wtedy działa funkcja przeglądarki history.back(). Funkcja
Zastosowanie router.subscribe() jako usługa: obsługa
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');
})
}
}
inject składnika, który inicjujący ułożenia:
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) {}
}
wstrzyknięcie tej usługi w innych składników, takich jak w tym przykładzie . Wtedy zmieniam stan, console.info() w serwisie nie działa.
Co robię źle?
Możliwy duplikat [Jak wykryć zmianę trasy w An gular 2?] (https://stackoverflow.com/questions/33520043/how-to-detect-a-route-change-in-angular-2) –