假设你的路由数组是这样构建的:
const routes: Routes = [
{
path: 'app', component: AppComponent
},
{
path: 'home', loadChildren: () => import('../features/home/home.module').then(x => x.Home)
},
{
path: 'moduleA', loadChildren: () => import('../features/moduleA/moduleA.module').then(x => x.ModuleA)
},
{
path: 'moduleB', loadChildren: () => import('../features/moduleB/moduleB.module').then(x => x.ModuleB)
},
{
path: 'moduleC', loadChildren: () => import('../features/moduleC/moduleC.module').then(x => x.ModuleC)
}
];
还有你的 Mode 枚举:
enum Mode {
ModuleA = "moduleA",
ModuleB = "moduleB",
ModuleC = "moduleC"
}
只需让 API 调用您的服务器,然后根据响应进行导航:
constructor(private http: HttpClient,private router: Router) { }
public navigate(internalRoute: string): void {
this.http.get<Mode>('/loadModule').subscribe(x => {
switch (x)
{
case Mode.ModuleA: this.router.navigateByUrl(`/${x}/${internalRoute}`); break
case Mode.ModuleB: this.router.navigateByUrl(`/${x}/${internalRoute}`); break
case Mode.ModuleC: this.router.navigateByUrl(`/${x}/${internalRoute}`); break
}
})
}