Reputation: 1765
I have a page where on clicking a button, it will redirect using below router navigation
router.navigate (['search', {data: 'test'}]).
But when I click the same button on 2nd time without changes the values, the router.navigate won't work. How can I override that.
All thoughts are welcome!
Upvotes: 3
Views: 10466
Reputation: 107
I faced a similar issue in Angular 9 application. The following worked for me
constructor(private route: ActivatedRoute) {
}
ngOnInit(): void {
this.paramSub = this.route.params.subscribe(
params => (this.Id = parseInt(params['Id']))
);
}
ngOnDestroy(){
this.paramSub.unsubscribe();
}
Upvotes: -1
Reputation: 1111
this._router.routeReuseStrategy.shouldReuseRoute = function(){
return false;
};
this._router.events.subscribe((evt) => {
if (evt instanceof NavigationEnd) {
this._router.navigated = false;
window.scrollTo(0, 0);
}
});
I added this to my app.component.ts ngOnInit
function, and it worked fine. All further clicks on the same link now reloads the component
and data.
Link to original GitHub feature request
Credit goes to mihaicux2 on GitHub.
I tested this on version 4.0.0-rc.3
with import { Router, NavigationEnd } from '@angular/router';
Upvotes: 7
Reputation: 7476
Angular router works this way, so if the activeRoute doesn't changed than the page/component won't be loaded again.
If you are passing parameters, like in your example: {data: 'test'} then you can watch these parameters changes by subscribing to route params:
constructor(private route: ActivatedRoute) {}
ngOnInit() {
this.route.params.subscribe(params => {
// each time the search data is change you'll get this running
//Do what ever you need to refresh your search page
console.log('New route params');
});
}
Upvotes: 5