我想根据一个标志定义不同的路线。这是
的一部分app-routing.module.ts
import { Token } from "..."; // InjectionToken
let routes: Routes;
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {
constructor(@Inject(Token) private token) {
if (this.token) {
routes = [...]
}
else {
routes =[...]
}
}
不幸的是,以上方法不起作用。知道如何实现我想要的吗?
答案 0 :(得分:1)
尝试如下设置路由器配置:
import { Token } from "..."; // InjectionToken
let routes: Routes;
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {
constructor(@Inject(Token) private token,
router: Router) {
let config = router.config;
if (this.token) {
routes = [...]
}
else {
routes =[...]
}
config = routes;
router.resetConfig(config);
}
}