I have angular application divided into various modules on the basis of user type. Like AdminModule
, CustomerModule
, VendorMoudle
etc. For each modules I've a separate respective shared module, having all the reusable items (components, pipes, directive etc). Like AdminSharedModule
, CustomerSharedModule
and VendorSharedModule
.
Along with each shared modules I've various featured modules inside each user level module which imports the respective user's shared module. For example, all featured modules of AdminModule
will import AdminShared
module. Similarly, all featured modules of Customer will import CustomerSharedModule
.
Now, I've a situation that there are some featured modules that belong to more than one user's module. Suppose, I've a featured module called CommonFeatureModule
. Now, I can create this module anywhere in the application & can call it lazily through routing in any of the module.
Now, here is the problem. As I explained, inside each featured module, I've imported the related User's Shared module. But for CommonFeatureModule
I've to conditionally import shared modules. Like if the CommonFeatureModule
is loaded through AdminModule
then the CommonFeatureModule
should import AdminSharedModule
. But if the CommonFeatureModule
is loaded in CustomerModule
then the CommonFeatureModule
should import CustomerSharedModule
.
I've tried creating a module called SharedModule
& inside this module I've used the static method forRoot
to conditionally load the respective user's shared module. But this solution didn't work. Her is the code for my SharedModule
:
@NgModule({
declarations: [SharedComponent],
imports: [],
exports: []
})
export class SharedComponent{
static forRoot(user: UserService): ModuleWithProviders<SharedModule> {
const data = {
ngModule: SharedModule,
imports: [
user.isAdminUser() ? AdminSharedModule : CustomerSharedModule
],
exports: [
user.isAdminUser ? AdminSharedModule : CustomerSharedModule
]
};
return data;
}
}