![](/img/trans.png)
[英]Angular 4.1.3 routing could not resolve child module from parent module
[英]Angular 8 routing to 404 using wildcard '**' in child module overrides parent module routing to other dynamic modules?
問題可能看起來很復雜,所以:
模塊設置:
- General (few common components and 404 page template)
- App (the app)
|___Main (main www)
| |___General (uses)
|___[DYNAMIC] Admin (admin panel)
|___General (uses)
路由模塊:
應用程序
基本上是<router-outlet></router-outlet>
,所有其他的東西都發生在子模塊中:
const routes: Routes = [
{ path: 'admin', loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule) }
];
@NgModule({
imports: [RouterModule.forRoot(routes, { enableTracing: true })],
exports: [RouterModule]
})
export class AppRoutingModule { }
主要的
由於Main
是App
模塊的子級,它的空路徑''
(或'/'
)將被放置在那里。 MainComponent
是帶有<router-outlet></router-outlet>
內容的頁眉。 默認情況下,它填充了HomeComponent
(路由''
),但也可以匹配到'/page1'
或'/page2'
。 現在請注意,有一個'**'
通配符應該捕獲所有不匹配的路徑並將它們顯示在MainComponent
插座中(這樣頁面內就有 404 帶有標題)。
const routes: Routes = [
{
path: '',
component: MainComponent,
children: [
{ path: '', component: HomeComponent },
{ path: 'page1', component: Page1Component },
{ path: 'page2', component: Page2Component },
{ path: '**', component: PageNotFoundComponent }
]
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class MainRoutingModule { }
管理員 [動態]
類似於Main
但從App
動態加載。 也有內部 404 通配符。
const routes: Routes = [
{
path: '',
component: AdminComponent,
children: [
{ path: 'page1', component: AdminPage1Component },
{ path: 'page2', component: AdminPage2Component },
{ path: '**', component: PageNotFoundComponent }
]
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class AdminRoutingModule { }
問題是,由於Main
路由以''
( '/'
) 開頭,它的 404 通配符'**'
MainComponent
了App
模塊的'/admin'
路徑,我們最終在MainComponent
得到 404(帶有標題的那個)。 在嘗試從MainComponent
鏈接到 /admin 以及在瀏覽器中輸入路徑時都會發生這種情況。 Main
模塊隱藏父路由。
要求是Main
模塊不能知道Admin
模塊(所以我不能從Main
模塊延遲加載 Admin 模塊,這就是為什么它被放置在App
模塊中),但同時( Main
模塊)可以有 404 的通配符.
我知道我可以創建其他路徑,例如:
main/page1
main/page2
admin/page1
admin/page2
...但我真的希望它是:
page1
page2
admin/page1
admin/page2
是否有子模塊通配符不影響父模塊的解決方案?
在app-routing.module.ts
指定main
模塊
如
const routes: Routes = [
{
'path': '',
'loadChildren': () => import('./modules/home/main.module').then(m => m.MainModule)
},
{ path: 'admin', loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule) }
];
確保它在管理模塊之上
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.