Suppose I have the following routes:
[
{
path: 'view',
children: [
{
path: ':id',
component: CustomerViewComponent,
resolve: {
customerViewData: CustomerResolve,
edit: ViewRouteResolve // Returns false
},
data: {
other: false
},
children: [
{
path: 'edit',
resolve: {
edit: EditRouteResolve // Returns true
},
data: {
other: true
}
},
{
path: '',
data: {
other: false
}
}
]
}
]
},
{ path: '', component: CustomerListComponent }
]
I want to use the CustomerViewComponent
for /view/ and /view/1/edit
The problem is I am unable to catch this data change in my component. I have tried with the resolve
or data
and I can't catch any changes...
This code will not trigger as expected:
this.route.data.subscribe(m => {
debugger; // Triggers only once the view loads, then never again
});
// This triggers quite often, but the data is always stale.
this.router.events.subscribe(m => {
console.log(this.route.snapshot.data['edit']);
console.log(this.route.snapshot.data['other']);
debugger;
});
Could this be a bug? my only work around is to look at the event NavigationEnd and analyze the .url string property...
Detect the change in URL route in NavigationStart’s subscribe method. We can add progress spinner or progress bar whenever a route change detected in Angular applications. Now we will go through an example to understand it further. I have created an Angular app which contains three routes. Aboutus,Services and Contactus.
The first approach is to tell the Angular router to emit the route events on refresh and then handle them accordingly in our routed component. In early Angular versions, there was no option to tell the router to emit events on same route refresh. Angular 5.1 introduced the onSameUrlNavigation property on the routers ExtraOptions.
Whenever there is a change in angular router, first it will call NavigationStart method as we have subscribed to it. Here we can create a variable which indicates whether we have to show loading indicators such as progress spinner or progress bar. After navigating to the required route, NavigationEnd event will be called.
The route data can be either static or dynamic. The static data use the Angular route data property, where you can store arbitrary data associated with this specific route. For to pass dynamic data (or an object), we can make use of the history state object. The Routed Component can then retrieve the dynamic data from the history state object.
When listening to router.events
the subscribe callback will get a few different types of events, multiple per route change. The ActivationStart
is the one that holds the route data
. Something like the following helped me:
this.router.events.subscribe(event => {
if (event instanceof ActivationStart) {
let data = event.snapshot.data;
this.titleService.setTitle(data['title'] || '_your_default_title_');
}
})
Try instead to use ActivatedRoute
from @angular/router
this.activatedRoute.params.subscribe(params => {
console.log(params['edit']);
console.log(params['other']);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With