问题
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.
回答1:
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
回答2:
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 () {
}
}
来源:https://stackoverflow.com/questions/47855240/angular-force-load-of-same-route