我想了解如何動態添加路由到那些來自延遲加載模塊。 我擁有核心應用,而在路線:Angular 2(Final):resetConfig將路由添加到延遲加載路由
export const routes = [{
path: "",
component: HomeComponent
}, {
path: "moduleA",
loadChildren: "app/moduleA/A.module"
}];
在A.module我從導入 「本地」 moduleA.routes.config.ts路線:
const routes = [{
path: "",
component: ModuleAHomeComponent,
children: [
{ path: "", component: ModuleAListComponent },
{ path: ":id", component: ModuleADetailComponent }
]
}];
export const routing = RouterModule.forChild(routes);
到目前爲止好。現在,在ModuleADetailComponent中,我有一個「附加模塊」列表,我將其稱爲插件,用於我的webapp,它們是單獨開發的,我希望能夠在ModuleADetailComponent頁面中顯示。 因此,此組件的模板是這樣的:
<div *ngFor="let plugin of plugins">
<div class="header">{{ plugin.name }}</div>
<router-outlet name="*plugin.name*">*LOAD MODULE HERE*</router-outlet>
</div>
當然,插件是在導航ModuleADetailComponent裝配置,所以我不知道提前哪些模塊的配置和有多少是。 最後,這些插件中的每一個都可以是一個NgModule(在工作臺環境中單獨開發)。
我需要的是:我該如何修改路由器配置,動態地將子項添加到moduleA.routes.config.ts中定義的路由中? 舉例來說,如果我有兩個插件,我要動態地使用router.resetConfig有那麼我有什麼,如果moduleA.routes.config.ts是這樣的:
const routes = [{
path: "",
component: ModuleAHomeComponent,
children: [
{ path: "", component: ModuleAListComponent },
{
path: ":id",
component: ModuleADetailComponent,
children: [
{
path: "plugin1",
loadChildren: "app/plugins/plugin1.module",
outlet: "plugin1"
},
{
path: "plugin2",
loadChildren: "app/plugins/plugin2.module",
outlet: "plugin2"
}
]
}
]
}];
export const routing = RouterModule.forChild(routes);
我希望我能實現它,否則插件將不得不進行開發,但它們可以成爲可從導航中受益的大型子模塊。
編輯27/09/2016:我已經組裝了這個plunkr:http://plnkr.co/edit/6aV5n5K5wdqdiYo49lZv。 我目前能夠做的是動態加載組件並將它們添加到應用程序中(請參閱「動態模塊」選項卡)。 儘管未找到任何官方文檔(請參閱「動態路由」選項卡),但我所做的還是使用多個命名的路由器插座。儘管標籤名稱,路線提前知道。
我想要做的是:動態加載配置(dynamicRoutes數組中的DynamicRoutesComponent),只有創建每個動態路由一個路由器出口後以及使用時resetConfig(或需要什麼)有這樣的配置:
{
path: "dynamic-routed",
component: DynamicRoutesComponent,
children: [
{ path: "", component: EmptyComponent },
{ path: "component1", component: Component1, outlet: "component1" },
{ path: "component2", component: Component2, outlet: "component2" }
]
}
每個動態路線有一個孩子。
在此先感謝!
非常感謝您提供詳細的答案,當我回到那個項目時,我一定會試着讓你知道它是否有效,現在我很傷心地轉向別的東西! – Etchelon
我無法使代碼正常工作。 Geetting NavigationError {ID:4,URL: 「/產品」,錯誤:ZoneAwareError} 這是featue模塊代碼 對象@NgModule({ 進口: SharedModule, RouterModule.forChild([]) ], 聲明: ProductListComponent, ProductDetailComponent, ProductEditComponent, ], 提供商: ProductService, ProductResolver, { 提供:路線, useValue:_routes, 多:真 }, { 提供:ANALYZE_FOR_ENTRY_COMPONENTS, useValue:_routes, 多:真 } ] }) – bomaboom