我的 Angular 应用程序模块有以下路由路径:
@NgModule({
imports: [
RouterModule.forChild([
{
path: 'documents',
data: { myObject: MyConstants.OPTION_ONE },
children: [
{
path: ':ID_DOC',
children: [
{ path: 'edit', component: EditDocumentComponent },
{ path: '', component: DocumentDetailsComponent },
]
},
{ path: 'add', component: AddDocumentComponent },
{ path: '', component: DocumentsListComponent }
]
}
])
],
exports: [
RouterModule
]
})
export class DocumentsManagementRoutingModule {
}
如您所见,我使用data
属性将一些数据传递给“文档”中的每个路径,因此我可以从路由路径中声明的任何组件中获取它:
例如,这是我获取数据的方式DocumentDetailsComponent
:
export class DocumentDetailsComponent implements OnDestroy {
private obsData: Subscription;
private option: any;
constructor(private route: ActivatedRoute) {
this.obsData = this.route.data.subscribe(data => {
this.option = data['myObject'];
});
}
ngOnDestroy(): void {
this.obsData.unsubscribe();
}
}
现在我改变了整个应用程序的路由结构,并从其他模块中调用上述模块,在延迟加载中,使用loadChildren
属性。我以同样的方式传递数据:
@NgModule({
imports: [
RouterModule.forChild([
{
path: 'users',
loadChildren: 'app/documents/documents.module#DocumentsModule',
data: { myObject: MyConstants.OPTION_ONE }},
{
path: ':ID_USER',
children: [
{ path: 'edit', component: EditUserComponent },
{ path: '', component: UserDetailsComponent },
]
},
{ path: 'add', component: AddUserComponent },
{ path: '', component: UserListComponent }
])
],
exports: [
RouterModule
]
})
export class UsersManagementRoutingModule {
}
根据我的需要,我对所有其他调用DocumentsManagementRoutingModule
、将myObject
属性更改为 等的模块都做了同样的事情。MyConstants.OPTION_TWO
MyConstants.OPTION_THREE
那么,由于我不知道调用我的延迟加载模块的模块及其相关路径,如何data
从调用者模块获取属性?