演示问题 https://stackblitz.com/edit/angular4-routeissue
https://angular4-routeissue.stackblitz.io/4566
我有一个类似localhost:4200/1234
的路由要求,其中1234
已映射到adNo。
我有localhost:4200/ad1
和localhost:4200/ad2
等子路线。 landComponent oninit方法获取参数并决定它在内部重定向的位置。
问题是,首次加载页面时会调用oninit方法两次,即加载localhost:4200/1234
时,如何防止这种情况。调用navigateByUrl时会发生两次调用。以下是路线代码
根路线
export const appRoutes: Routes = [
{ path: ':adNo', component: landComponent },
];
儿童路线
export const subRoutes: Routes = [
{
path: '',
component: landComponent,
children: [{ path: 'ad1', component: infoComponent },
{ path: 'ad2', component: typeComponent }]
}
];
LandComponent
ngOnInit() {
// logic to get the adNo and make service call
this._activatedRoute.snapshot.params['adNo'];
console.log('working');
this._router.navigateByUrl('ad1', { skipLocationChange: true });
}