Is there a way to trigger events on every URL change within a child router? I have been able to implement event firing on URL changes within the main router using the following code, but I am struggling with achieving this in a child router.
I'm currently working with router-deprecated and Angular2 beta versions, not RC.
// myapp.component.ts
@RouteConfig([
{
useAsDefault: true,
name: 'Main',
path: '/...',
component: MainComponent,
},
])
export class MyAppComponent {
constructor(private _router: Router) {
_router.subscribe((url: string): void => {
// Trigger event on every URL change
// e.g. from "/" to "/sub1", or "/sub1" to "/sub2"
});
}
}
// main.component.ts
// URL: /
@RouteConfig([
{
useAsDefault: true,
name: 'Sub1',
path: '/sub1',
component: Sub1Component,
},
{
name: 'Sub2',
path: '/sub2',
component: Sub2Component,
},
])
export class MainComponent {
constructor(private _router: Router) {
_router.subscribe((url: string): void => {
// Event not triggered...
});
}
}
// sub1.component.ts
// URL: /sub1
export class Sub1Component {}
// sub2.component.ts
// URL: /sub2
export class Sub2Component {}
(Please note that import
syntax and Decorators were omitted in the above code.)