AngularM AngularM - 7 months ago 20
Javascript Question

Angular 2.0 - How to detect route change?

Angular 2.0 - How to detect route change?

I looking to detect route change in my App.ts file.

Once I've done this I will check the global user token to see if they are logged in. Then I can redirect the user if they are not logged in.

Thank you in advance!

Answer

In Angular 2 you can subscribe (Rx event) to a Router instance. So you can do things like

class MyClass {
  constructor(private router: Router) {
    router.subscribe((val) => /*whatever*/)
  }
}