1
我目前被困在角度路由如何工作,需要一些幫助。基本上在我的項目,我有一個核心模塊,用於加載所有的根路徑,家是以下幾點:Angular Routing懶惰加載兒童的孩子
const routes: Routes = [
{path: '', redirectTo: '/login', pathMatch: 'full'},
{path: 'user', loadChildren: 'app/userManagement#UserModule', pathMatch: 'full',canActivate: [AuthGaurd] },
{path: '**', component: PageNotFoundComponent}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}
而且在app/userManagement文件夾我已經使用了進口的index.ts並聲明所有模塊:
@NgModule({
imports: [
SharedModule,
UserManagementRoutingModule
],
declarations: [UserHomeComponent, UserListComponent, UserDetailsComponent]
})
export class UserModule {
}
而且我的孩子路由把UserManagementRoutingModule內:
const routes: Routes = [
{
path: '',
component: UserHomeComponent,
},
{
path: 'userDetails',
component: UserDetailsComponent
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class UserManagementRoutingModule {
}
這樣,當我去http://hostname/user就會直接ŧ o我的UserHomeComponent組件,但是如果我去http://hostname/user/userDetails Angular沒有重定向到該頁面。我應該如何編輯我的代碼,以便可以訪問userDetailsComponent?
感謝
我不認爲你的解決方案延遲加載。 – omeralper
啊對不起,你剛剛完成了他的代碼,現在我明白了。 – omeralper
這是正確的方法,實際上我還需要刪除「pathMatch:'full'」語句,然後代碼正常工作。謝謝! –