Basically I have a notification component that show notification on click of icon in header. When we click the icon on header, it opens a popup and shows a list of notification. On clicking any one of the notification, it routes the user to the component of that particular notification, like on clicking notification , it takes user to that route : profile/guest/{guestid}/alerts/{alertid}. But when we click on another notification from the above mentioned route , it chnages the route params but does not reload the data of the new alert and shows the data of the old route only.
Note: data that are shown are fetched from route resolve. And as the component is already loaded , it doesn't call ngOnInit or constructor function when we click on another notification. But when we refresh the page , it show correct data according to the updated route.
I tried implementing different router config solution that reloads the resolves data. such as runGuardsAndResolvers along with onSameUrlNavigation.
I also tried calling the functions that sets data, in other angular component life cycle hooks such as ngAfterViewInit or ngAfterViewChecked.
I tried a few more solutions, but all of them were of no use.
Notification.component.ts :-(inside header component in shared module)
/**
* * Callback when user clicks on visit button in notification modal
* @param notification : notification
*/
navigateToGuestOrCustomer(notification: notification) {
let routePath =
notification.model == ALERT_MODEL.GUEST ? "guest" : "customers";
let routeParamId = notification.detail_id;
let alertId = notification.id;
this.router.navigate([`/profile/${routePath}/${routeParamId}/alerts/${alertId}`]);
}
edit-alert.component.ts(inside profile module in guest component>alert component)
ngOnInit() {
this.fetchRouteData();
this.setGuestId();
this.setGuestName();
}
/**
* * get guest name from route resolve
*/
setGuestName(): void {
let guestData = this.route.parent.snapshot.data["editGuestInfo"];
this.guestName = guestData["name"];
}
formData: any;
alertListForGuest = [];
/**Fetch Data from Resolve */
fetchRouteData() {
this.formData = this.route.snapshot.data["alertDetailForGuest"];
this.alertListForGuest = this.route.snapshot.data["alertListForGuest"];
this.alertListForGuest.push(this.formData.alert);
}
expected result : after clicking on other notification , route parameters should change and show the desired page with the updated data.
actual result : only the route is getting changed , not the data.
ngOnInit
might not be called again if only the route changes, but the component is not recreated.
You should subscribe to the route changes and perform your fetch and update logic there
import { ActivatedRoute } from '@angular/router`
constructor(private activatedRoute : ActivatedRoute){ }
ngOnInit() {
this.activatedRoute.url.subscribe(url =>{
// Code to get the new notification data
// and display it
});
}
In this way when you navigate to a new url, the code is execute again
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