我對 Angular 很陌生,我真的需要路由方面的幫助。這是我的檔案夾結構:

有兩種不同的布局,一種是登錄頁面的布局,一種是主布局,如通常的管理頁面,有側邊欄、工具列等。
登錄后,我希望它會加載帶有主布局的儀表板頁面,但它只顯示布局而不顯示儀表板。
應用程式路由.module.ts
const accountModule = () => import('./features/account/account.module').then(x => x.AccountModule);
const mainModule = () => import('./features/main/main.module').then(x => x.MainModule);
const routes: Routes = [
{ path: '', loadChildren: mainModule, canActivate: [AuthGuard] },
{ path: 'account', loadChildren: accountModule },
// otherwise redirect to home
{ path: '**', redirectTo: '' }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
帳戶路由.module.ts
const routes: Routes = [
{
path: '', component: LayoutComponent,
children: [
{ path: 'login', component: LoginComponent },
]
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
主路由.module.ts
const routes: Routes = [
{
path: '', component: LayoutComponent,
children: [
{ path: 'dashboard', component: DashboardComponent },
{ path: 'client', component: ClientComponent },
]
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
如何修改路由,以便當我訪問 http://localhost:4200 時,它將顯示帶有主布局的儀表板頁面,而不僅僅是布局?或者如果不可能,將其重定向到 http://localhost:4200/dashboard?請幫忙。
uj5u.com熱心網友回復:
在您的 main-routing.module.ts 中,使用帶有空字串的完整匹配路徑重定向您所需的路徑。
const routes: Routes = [
{
path: '', component: LayoutComponent,
children: [
{ path: 'dashboard', component: DashboardComponent },
{ path: 'client', component: ClientComponent },
]
},
{ path: '', redirectTo: 'dashboard', pathMatch: 'full' },
];
編輯:更正順序
const routes: Routes = [
{ path: '', redirectTo: 'dashboard', pathMatch: 'full' },
{
path: '', component: LayoutComponent,
children: [
{ path: 'dashboard', component: DashboardComponent },
{ path: 'client', component: ClientComponent },
]
},
];
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/436738.html
上一篇:使用服務檔案角度保存資料
