I'm new in angular and I was wondering if it's possible to load and module and its components I made based on a conditional on the app.module or where would it be the best place to do this.
Basically I want to do something like:
if(user.deparment === 'coms') {
//Use the communications.module and its components
}
I have attached some picture so you guys can see the structure of the app. if necessary I can add the code instead of a picture.
App.module picture
Communications.module picture
This root NgModule is what's used to bootstrap the Angular application. It is in this root module that we also bootstrap the root-level component. This root-level component is the application's main view, which hosts other components for the application. An NgModule is a class adorned with the @NgModule decorator.
You can export any declarable class —components, directives, and pipes— whether it's declared in this NgModule or in an imported NgModule. You can re-export entire imported NgModules, which effectively re-export all of their exported classes. An NgModule can even export a module that it doesn't import.
@NgModule takes a metadata object that describes how to compile a component's template and how to create an injector at runtime. It identifies the module's own components, directives, and pipes, making some of them public, through the exports property, so that external components can use them.
There are two types of modules, root modules and feature modules.
You can do a simple ternary check to conditionally import a module. Like this:
import { NgModule, Optional } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
@NgModule({}) class MyModule {}
// toggle and watch the console
const production = true;
@NgModule({
imports: [ BrowserModule, production ? MyModule : []],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule {
constructor(@Optional() module: MyModule) {
console.log(module);
}
}
Live demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With