角2路进口模块覆盖当前
问题描述:
这里的plunker:https://plnkr.co/edit/WIFNVIEVqls4gXk21Muj角2路进口模块覆盖当前
有是2个模块都有定义的路由。模块2导入模块1以便使用它的一个组件。您无法导航到模块2.模块1加载代替。
模块1个路由:
const routes: Routes = [
{ path: '', component: Module1Component }
];
模块2路由:
const routes: Routes = [
{ path: '', component: Module2Component }
];
应用路由:
const routes: Routes = [
{ path: 'module1', loadChildren: 'app/module1/module1.module#Module1Module' },
{ path: 'module2', loadChildren: 'app/module2/module2.module#Module2Module' }
];
谢谢。
答
两个问题得到这个工作。首先,路由器存在一个错误,它会考虑导入模块的顺序。有关错误的更多详细信息,请参阅此错误报告:https://github.com/angular/angular/issues/12648
因此,为了避开该错误,您需要更改module2.module.ts中的导入顺序,以便在导入之前导入您的module2路由文件模块1。
@NgModule({
imports: [
routing, // this order is important
Module1Module
],
declarations: [
Module2Component
]
})
二,而不是用你的出口路由模块作为一个const ModuleWithProviders其导出为一类。又名改变模块2路由文件(module2.routing.ts)出口一类像这样(你需要导入NgModule):
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class routing {
}
这应该可以解决它。这里有一个工作plunker:https://plnkr.co/edit/5impstull9EBCUxlw26k
提供代码和解释它的好工作。真正提高你答案的质量。 –
谢谢。我看到订单问题,但它作为一个班级出口有什么不同? – rook
我没有深入了解为什么你需要将路由模块作为一个类导出。我刚刚注意到在最新的角度文档中,他们的例子都将其导出为一个类[https://angular.io/docs/ts/latest/guide/router.html](https://angular.io/docs/ts /latest/guide/router.html)。当我做出改变时,它开始工作。可能角度不赞成其他方法?不确定。 – eddierunner