I have a route that goes to a calculator. The person might hit calculate and the screen shows the result. Which is fine. They then may go up to the navbar and click the same calculator again, by that I mean they are clicking the same route. I would like to screen to re-route so they are in essence starting over, refreshing the screen. Of course the route does not change nor does the screen change because they are on the same route. Is there some way to force the route to re-load or refresh. I have several different routes with different components that need this. So if I need to write code to handle them all I would like it to be in a service and not in the component. Preferably I would like it when I am setting up the routes.
Asked
Active
Viewed 1,971 times
0
-
Can you try this solution - https://stackoverflow.com/a/45737269/2030471 – Dec 17 '17 at 13:00
-
Yes that did work, though I am not sure putting that in every component you want to use it in is the best idea, I might have to put it in a service or a class I can inherit it from – Maccurt Dec 17 '17 at 21:30
2 Answers
2
One of the cool features of Angular 5 is precisely the ability to reload/refresh the same page in case you want it. You just need to set it on your configuration
providers: [
// ...
RouterModule.forRoot(routes, {
onSameUrlNavigation: 'reload'
})
]
It’s part of what’s called ExtraOptions for router. You can check the documentation here if you need more details

Hugo Noro
- 3,145
- 2
- 13
- 19
-
This looks promising. I will have to make sure it does not do it on all routes. I would like to pick and choose what routes. I do it on. I was hoping I can do this on each individual route.. thanks, I will look into this.. – Maccurt Dec 17 '17 at 13:24
-
I will try this once I get out of church, I will have to upgrade to 5.x and I will mark this as the answer if it works out for me – Maccurt Dec 17 '17 at 14:19
-
-
-
0
Implement OnInit
and call ngOnInit()
in the method for route.navigate()
See an example :
export class Component implements OnInit {
constructor() { }
refresh() {
this.router.navigate(['same-route-here']);
this.ngOnInit(); }
ngOnInit () {
}
}

Evandro Mendes
- 116
- 3