I have a feature module that declares components and also configures routing through a static function.
@NgModule({
declarations: FEATURE_COMPONENTS,
entryComponents: FEATURE_COMPONENTS,
imports: [
UIRouterModule,
...
]
})
export class FeatureModule {
public static setParentState(stateName: string): ModuleWithProviders {
...
return {
ngModule: FeatureModule,
providers: [
...UIRouterModule.forChild({ states: ... }).providers
// my providers
{
provide: FEATURE_PROVIDER_TOKEN,
useValue: ...
multi: false
}
]
};
}
}
Simply importing the module isn't enough, correct configuration is done by calling the static function.
I want to avoid incorrect usage like this:
@NgModule({
...
imports: [
// invalid import - should be prevented
FeatureModule
// valid import
FeatureModule.setParentState(...)
]
})
export class SomeAppModule {}
How can I enforce using the static function for importing the feature module? The goal is to prevent direct importing and guide users to use the correct method...
One option could be leveraging the additional provider added in the static function. But how? Through the module constructor and injections? Maybe with a custom decorator?
I was considering something along these lines, but also including a custom error message with instructions for proper import:
@NgModule({ ... })
export class FeatureModule {
public static setParentState(stateName: string): ModuleWithProviders {
// FEATURE_PROVIDER_TOKEN defined here (see above)
...
}
constructor(
@Import(FEATURE_PROVIDER_TOKEN) tokenTest: any
} {
// This will throw an error if the provider isn't defined but won't allow a specific error message to be set
}
}
Any suggestions or ideas?