I have an Angular Application. In my app.module.ts
declaration I would do something like this:
let imports = [
//....
];
if (!environment.production) {
imports.push(DevToolsModule);
}
@NgModule({
imports: imports,
...
})
My separate feature module DevToolsModule
looks like the following:
@NgModule({
declarations: [DeveloperToolsComponent],
imports: [
CommonModule
],
entryComponents: [DeveloperToolsComponent]
})
export class DevToolsModule {}
What I would do now, is the following:
When my DevToolsModule
gets imported, I would like to show a component on my screen with some developer information. It should be automatically added, I don't want to define it anywhere in my app.html
template.
Did anyone achieve something like that?
Thanks so much, Sebastian
You can do the following,
let developmentModules = [
DevToolsModule
]
if(environment.production) {
developmentModules = [];
enableProdMode();
}
@NgModule({
imports: [
BrowserModule,
...developmentModules
],
declarations: [],
providers: [],
})
export class AppModule
The spread operator ...
allows to easily place an expanded version of an array into another array.
When you import your feature module in root module, it will automatically be added to tree. You don't need to edit app.component.html template for that, but to demonstration, you should. If you can't edit app.component.html template, you can add the feature module component dynamically as @viewchild instance and ComponentFactoryResolver in app.component.ts, but I wouldn't recommend it. Instead you should define the feature module component as a decleration, then use it in template
<child-component></child-component>
Anyway if you still find this silly, you might want checkout the article: Dynamically add components to the DOM with Angular
Theoretically thinking of this:
constructor(@Optional() devToolsModule:DevToolsModule ) {
if(devToolsModule){
//add component to view
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.