加载要素模块时Angular匹配路由的顺序是什么?

时间:2017-09-28 19:30:46

标签: angular

当我启动我的应用程序时,它导航到ItdeptComponent,但我原以为它会路由到HomeComponent?

假设我为根应用程序定义了路由,如下所示:

const appRoutes: Routes = [    
    { path: 'home', component: HomeComponent },
    { path: 'it', component: ItdeptComponent },
    { path: '', component: HomeComponent, pathMatch: 'full' }
];
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);

然后,在功能模块中,有一些像这样的路线

const itappRoutes: Routes = [
    { path: 'it', component: ItdeptComponent },
    { path: 'itblog', component: ItBlogPostListComponent },
    { path: 'editpost', component: EditBlogPostComponent },
    { path: 'createpost', component: CreateBlogPostComponent },
    { path: '', component: ItdeptComponent, pathMatch: 'full' }
];

export const routing: ModuleWithProviders = RouterModule.forChild(itappRoutes);

[更新

root module routes<br/>
const appRoutes: Routes = [    
    { path: 'home', component: HomeComponent },
    { path: 'it', component: ItdeptComponent },
    { path: '', redirectTo: '/home', pathMatch: 'full' }
];

特征模块路线:

const itappRoutes: Routes = [
    { path: 'it', component: ItdeptComponent },
    { path: 'itblog', component: ItBlogPostListComponent },
    { path: 'editpost', component: EditBlogPostComponent },
    { path: 'createpost', component: CreateBlogPostComponent },
    ***{ path: '', redirectTo:'/it', pathMatch: 'full' } ];***

***删除最后一条路线

1 个答案:

答案 0 :(得分:1)

您必须使用redirectTo而不是仅指定组件

{ path: '', redirectTo: '/home', pathMatch: 'full' },

{ path: '', redirectTo: '/it', pathMatch: 'full' },

在各自的模块中。希望它有所帮助。