Trying to redirect to a new component from one of the hooks like ngOnInit(), ngAfterViewInit() based on a param supplied. The current component is executed multiple times if the router navigate line is hit.
worked solution:
setTimeout(() -> { this.router.navigate(link)); //recently upgraded angular 3 to 4.0.0 router
main module :
RouterModule.forChild([
{ path: 'referrer/:vehicle/:referrer', component: ReferrerComponent },
{ path: 'referrer/:referrer', component: ReferrerComponent },
{ path: 'vehicle', component: vehicleComponent},
])
route: http://localhost:4200/#/referrer/vehicle1/referrer1
ReferrerComponent:
ngOnInit() {
this.routeObservable = this.route.params.subscribe((params) => {
if(params["referrer"]) { // do referrer stuff;}
if(params["vehicle"] == "vehicle1") {
this.router.navigate('/vehicle'); //redirect to vehicle component
}
});
}
As this happens only if a redirect actually happens, not sure where to put the re route (lifecycle) exactly.