Angular 4动态路由
发布时间:2020-12-17 07:16:23 所属栏目:安全 来源:网络整理
导读:我想在angular 4应用程序中实现动态路由.我想要做的是将新的Route对象推送到Router config.代码看起来像 @NgModule({declarations: [ AppViewComponent],imports: [ RouterModule.forRoot([ { path: '',redirectTo: 'home',pathMatch: 'full' },{ path: 'hom
我想在angular 4应用程序中实现动态路由.我想要做的是将新的Route对象推送到Router config.代码看起来像
@ NgModule({ declarations: [ AppViewComponent ],imports: [ RouterModule.forRoot([ { path: '',redirectTo: 'home',pathMatch: 'full' },{ path: 'home',component: HomeComponent },{ path: 'appview',component: AppViewComponent } { path: '**',redirectTo: 'home' } ]) ],providers: []}) export class AppModuleShared { constructor(router: Router,routerModule: RouterModule) { console.log('Routes: ',JSON.stringify(router.config,undefined,1)); router.config.push({ path: 'new/random/path',component: AppViewComponent }); router.config.forEach((x,i) => { console.log(`${i}: ${JSON.stringify(x,1)}`); }); }} 当应用程序启动时,我想将新的Route Object推送到配置并转到构造函数中的新路径.执行构造函数后,在控制台日志中我有这样的事情: 5: { "path": "new/random/path" } 看起来新的Route Object被成功推送到配置数组,但是当我尝试进入这个新的路径时,应用程序将我重定向到主页.你能告诉我我做错了什么吗? 解决方法
使用unshift而不是push
例:router.config.unshift({path:’new / random / path’,component:AppViewComponent}); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |