我正在寻找一种执行这种情况的方法:
export const config = {
authorization: {},
navigation: {
title: 'Test page',
homeRoute: 'home',
actions: [{ title: 'Do stuff', route: '/settings' }]
},
settings: {
groups: [
{
groupName: 'Advanced',
options: [
{
name: 'Settings Option Route',
route: 'test',
component: TestComponent
}
]
}
]
}
};
@NgModule({
declarations: [AppComponent, TestComponent],
imports: [
BrowserModule,
AppRoutingModule,
ThatImportedLibrary['forRoot'](config)
],
exports: [TestComponent],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}
库中的
Settings组件取出有关注入对象中路由的数据,并基于该数据重置子路由(通过使用主 settings 路由并从 config对象添加子组件)。
export const settingsRoutes: Routes = [
{
path: 'settings',
component: SettingsPanelComponent,
children: []
}
];
@NgModule({
imports: [RouterModule.forChild(settingsRoutes)],
exports: [RouterModule]
})
export class SettingsPanelRoutingModule {
constructor(@Inject(ConfigService) private config: FacConfig) {
// RouterModule.forChild(settingsRoutes);
// Looking for a way to dynamically add the child route from config object
const settingsRoutes = [
{
path: 'settings',
component: SettingsPanelComponent,
children: []
}
];
const childRoute = {
path: config.settings.groups[0].options[0].route
component: config.settings.groups[0].options[0].component
}
settingsRoutes[0].children.push(childRoute);
this.router.resetConfig(settingsRoutes);
}
}
它不起作用,但是-在主模块(TestComponent)中单击带有子组件的选项后,它会导致:
ERROR Error: No component factory found for TestComponent. Did you add it to @NgModule.entryComponents? at noComponentFactoryError (component_factory_resolver.ts:17)
答案 0 :(得分:0)
好吧,我自己回答了-在主app.module中,我只需要将该特定模块添加到@NgModule中的 entryComponents 属性中即可。
@NgModule({
declarations: [AppComponent, TestComponent],
imports: [
BrowserModule,
AppRoutingModule,
ThatImportedLibrary["forRoot"](config)
],
exports: [TestComponent],
providers: [],
bootstrap: [AppComponent],
entryComponents: [TestComponent] // that's all!
})