Arūnas Smaliukas Arūnas Smaliukas -4 years ago 281
TypeScript Question

Angular2 watch for route change

{path: '/about', name: 'About', component: About, useAsDefault: true},
{path: '/test', name: 'Test', component: Test}

export class MyApp {
router: Router;
location: Location;
isCollapsed: boolean = true;

this.isCollapsed = true;
// }

constructor(router: Router, location: Location) {
this.router = router;
this.location = location;

I need change variable value on every route change, how to watch for this event in Angular 2.x?

Answer Source

Here's what I use in my app. You can subscribe to a Route instance to track changes.

class MyClass {
  constructor(private router: Router) {
    router.subscribe((val) => /*detect changes*/)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download