【发布时间】:2018-08-18 08:07:35
【问题描述】:
我在 AppComponent 中有一个路由器插座,可以很好地加载子组件。其中一个子组件有多个子组件,我想将所有子组件加载到父路由器插座中,如下所示
AppComponent <router-outlet> (This loads all child including products)
Customers (component)
Pricing (component)
Products (component) <router-outlet name="products"> (This should load child products)
TypeProduct1 (Component)
TypeProduct2 (Component)
但我所有的子产品都加载在主路由器插座中,例如当我输入网址时
http://localhost:4200/products/typeproduct1 - 它成功加载 TypeProduct1 组件,但在主路由器出口而不是产品路由器出口。
我在路由中懒惰地加载 Products 模块。会不会是这个问题?
编辑:
路线如下所示:
AppRoutingModule:
const routes:Route = [
{ path: 'customers', loadChildren:'/path/to/customers/module' },
{ path: 'pricing', loadChildren:'/path/to/pricing/module' }
{ path: 'products', loadChildren: 'path/to/products/module' }
];
@NgModule({
imports: [RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules, useHash: true })],
exports: [RouterModule],
declarations: [NotFoundComponent]
})
ProductRoutingModule 看起来像这样:
const routes: Routes = [
{ path: '', component: ProductsComponent },
{ path: 'type1', loadChildren: 'path/to/Type1product/module' },
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
所有组件都加载到顶级路由器插座中。我希望 type1 模块加载到产品路由器插座中。
【问题讨论】:
-
延迟加载应该不是问题。你的路由配置是什么样的?此外,如果您使用命名(也称为辅助或辅助路由),使用它们的语法是不同的。您可能希望删除
name属性并仅使用嵌套(也称为子)路由。
标签: angular