Pic*_*cci 5 angular2-routing angular
我试图在运行时向Angular2路由器添加一些配置.
我正在做的很简单.在AppModule中,我按照指南加载路由器的初始配置
的AppModule
@NgModule({
declarations: [
...
],
imports: [
BrowserModule,
routing,
...
],
providers: [appRoutingProviders],
bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)
appRoutes - app.routes.ts
const appRoutes: Routes = [
{ path: '', component: PocWelcomeComponent },
{ path: '**', component: PageNotFoundComponent }
];
export const appRoutingProviders: any[] = [
];
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);
Run Code Online (Sandbox Code Playgroud)
然后在AppComponent onInit()方法中添加一些这样的路由
AppComponent
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);
}
}
Run Code Online (Sandbox Code Playgroud)
当我现在尝试使用代码导航到function1时,this.router.navigate(['/payment']);我将路由到PageNotFoundComponent.
相反,如果我在AppModule的配置中将路径的配置放到function1,一切正常.
我也在使用Angular-CLI.
很感谢任何形式的帮助.提前致谢
试试这段代码:
let r: Route = {
path: 'pop',
component: PopupComponent
};
this.router.resetConfig([r, ...this.router.config]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2338 次 |
| 最近记录: |