【发布时间】:2016-10-06 11:38:52
【问题描述】:
我正在尝试在运行时向 Angular2 Router 添加一些配置。
我所做的很简单。在AppModule 中,我按照指南加载Router 的初始配置:
AppModule
@NgModule({
declarations: [
...
],
imports: [
BrowserModule,
routing,
...
],
providers: [appRoutingProviders],
bootstrap: [AppComponent]
})
export class AppModule { }
appRoutes -- app.routes.ts
const appRoutes: Routes = [
{ path: '', component: PocWelcomeComponent },
{ path: '**', component: PageNotFoundComponent }
];
export const appRoutingProviders: any[] = [
];
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);
然后在 AppComponent onInit() 方法中添加一些这样的路由
应用组件
export class AppComponent implements OnInit {
constructor(
private router: Router
) {}
ngOnInit() {
let routes = this.router.config;
routes.push({ path: 'function1', component: Function1Component });
this.router.resetConfig(routes);
}
}
当我现在尝试使用代码 this.router.navigate(['/payment']); 导航到 function1 时,我被路由到 PageNotFoundComponent。
相反,如果我把function1的路径配置放在AppModule的配置中,一切正常。
我也在使用 Angular-CLI。
非常感谢任何帮助。提前致谢
【问题讨论】: