Me pregunto si hay una forma de ejecutar algo después de navegar a una "vista" diferente usando un enrutador angular.
this.router.navigate(["/search", "1", ""]); // Everything after navigate does not not get executed. this.sideFiltersService.discoverFilter(category);this.router.navigate devuelve una promesa para que simplemente pueda usar:
this.router.navigate(["/search", "1", ""]).then(()=>{ // do whatever you need after navigation succeeds });// In javascript this.router.navigate(["/search", "1", ""]) .then(succeeded => { if(succeeded) { // Do your stuff } else { // Do some other stuff } }) .catch(error => { // Handle the error }); // In typescript you can use the javascript example as well. // But you can also do: try { let succeeded = await this.router.navigate(["/search", "1", ""]); if(succeeded) { // Do your stuff } else { // Do some other stuff } } catch(error) { // Handle the error }No estoy del todo seguro del contexto, pero una opción sería suscribirse a un cambio en la URL usando ActivatedRoute
https://angular.io/docs/ts/latest/guide/router.html#!#ruta activada
Aquí hay un ejemplo:
... import { ActivatedRoute } from '@angular/router'; ... private _routerSubscription: any; // Some class or service constructor(private _route: ActivatedRoute){ this._routerSubscription = this._route.url.subscribe(url => { // Your action/function will go here }); } Hay muchos otros observables a los que puede suscribirse en ActivatedRoute que se enumeran en ese enlace si la URL no es exactamente lo que necesita.
La suscripción se puede hacer en el constructor() o en un ngOnInit() dependiendo de lo que más le convenga, solo recuerde limpiar después de usted mismo y darse de baja en un ngOnDestroy() :)
this._routerSubscription.unsubscribe();