我正在用解析器的数据加载一个模块。加载的模块具有通常的空路径重定向到第一个子路由。但我想根据解析的数据重定向到特定的路由。
在当前的示例中,我有一个包含三个组件的子模块。根模块中的Resolver提供应该重定向到哪个组件的数据。
AppModule:
const routes = [
{
path: '',
redirectTo: 'steps',
pathMatch: 'full'
}, {
path: 'steps',
loadChildren: './steps/steps.module#StepsModule',
resolve: {
step: StepResolver,
}
},
]
@NgModule({
imports: [ BrowserModule, RouterModule.forRoot(routes) ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }ChildModule:
const routes = [
{
path: '',
redirectTo: 'step1', // <-- this has to be dynamic based on the provided data
pathMatch: 'full'
}, {
path: 'step1',
component: Step1Component
}, {
path: 'step2',
component: Step2Component
}, {
path: 'step3',
component: Step3Component
},
]
@NgModule({
imports: [ CommonModule, RouterModule.forChild(routes) ],
declarations: [ Step1Component, Step2Component, Step3Component ],
})
export class StepsModule { }Resolver返回应该重定向到的步骤。
@Injectable({
providedIn: 'root'
})
export class StepResolver implements Resolve<string> {
resolve(route: ActivatedRouteSnapshot): Observable<string> {
return of('step2');
}
}如何根据提供的数据重定向到路由?有我可以使用的路由钩吗?
通过:
发布于 2019-08-12 08:50:48
您可以评估下一步需要什么,然后导航到特定的路由:
this.route.navigate([yourStep]);您必须导入Router:
constructor(private route: Router) { }编辑:
使用以下内容创建一个服务:
goToStep(step: string) {
this.route.navigate([step]);
}考虑到您提供的代码,解析器:
resolve(route: ActivatedRouteSnapshot): Observable<string> {
return this.stepService.goToStep('step2');
}https://stackoverflow.com/questions/57457488
复制相似问题