Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 router event listener

How to listen state change in Angular 2 router?

In Angular 1.x I used this event:

$rootScope.$on('$stateChangeStart',     function(event,toState,toParams,fromState,fromParams, options){ ... }) 

So, if I use this eventlistener in Angular 2:

window.addEventListener("hashchange", () => {return console.log('ok')}, false); 

it isn't return 'ok', then change state from JS, only then browser history.back() function run.

Use router.subscribe() function as the service:

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 service in component which init in routing:

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

I inject this service in other components such as in this example. Then I change state, console.info() in service not working.

What I do wrong?

like image 824
Yegor Avatar asked Mar 10 '16 09:03

Yegor


People also ask

What is navigateByUrl in Angular?

navigateByUrl is similar to changing the location bar directly–we are providing the “whole” new URL. Whereas router. navigate creates a new URL by applying an array of passed-in commands, a patch, to the current URL.

What is NavigationEnd in Angular?

NavigationEndlinkAn event triggered when a navigation ends successfully. class NavigationEnd extends RouterEvent { constructor(id: number, url: string, urlAfterRedirects: string) type: EventType.

What is the difference between navigate and navigateByUrl in Angular?

It has two methods, navigate and navigateByUrl , that navigate the routes. They are similar; the only difference is that the navigate method takes an array that joins together and works as the URL, and the navigateByUrl method takes an absolute path.


1 Answers

new router

constructor(router:Router) {   router.events.subscribe(event:Event => {     if(event instanceof NavigationStart) {     }     // NavigationEnd     // NavigationCancel     // NavigationError     // RoutesRecognized   }); } 

old

Inject the Router and subscribe to route change events

import {Router} from 'angular2/router';  class MyComponent {   constructor(router:Router) {     router.subscribe(...)   } } 

NOTE

For the new router, don't forget to import NavigationStart from router module

import { Router, NavigationStart } from '@angular/router'; 

because if you don't import it instanceof will not work and an error NavigationStart is not defined will rise.

See also

  • https://angular.io/docs/ts/latest/api/router/index/Router-class.html
  • How to detect a route change in Angular 2?
like image 149
Günter Zöchbauer Avatar answered Oct 16 '22 08:10

Günter Zöchbauer