Angular 2 routing to the same route

2020-04-07 18:50发布

问题:

I have Ride component, and in the end of the ride the Ride Summary is shown.

The right summary has a button to "Start another ride" which redirects to the same component, Ride.

[routerLink]="['Ride']"

Since the Ride component is already the one i'm in, it's not refreshed and the Ride Summary screen is keep showing.

How I can re-init the Ride component when re-navigating to it? (use again the ngOnInit method)

回答1:

Router doesn't navigate if the route and params haven't changed, by default.


To make it navigate you can define

routerCanReuse(nextInstruction: ComponentInstruction, 
               prevInstruction: ComponentInstruction){
  return false;
}

in your component, that you want to renvaigate to, and call

Router.renavigate();

this.router.navigate('RouteName', this.routeParams.params)

to renavigate.



标签: angular