I am working on an application that consists of various components, each defining its own subset of route states. Depending on the context in which these components are used, a parent state needs to be provided under which some component states should be nested.
For example, here are the component states:
[{
name: 'component',
url: '/component',
component: ComponentRootComponent,
abstract: true,
default: '.list'
}, {
name: 'component.list',
url: '',
component: ComponentListComponent
},...]
In one specific section module of the app:
@NgModule({
declarations: ...,
imports: [
UIRouterModule.forChild({ states: SECTION_STATES }),
SomeComponentModule,
UIRouterModule.forChild({
states: RouterUtil.setParentState(SECTION_STATES[0].name, SOME_COMPONENT_STATES)
})
]
})
export class AppSectionModule {}
The setParentState()
function prefixes component states with the parent state name, ensuring they fall under that specific route state.
Instead of manually importing modules for each component, I want to create a single and configurable import function per component within my modules.
@NgModule({
declarations: ...,
imports: [
UIRouterModule.forChild({ states: SECTION_STATES }),
SomeComponentModule.importFor(SECTION_STATES[0].name)
]
})
export class AppSectionModule {}
The importFor
function should return a ModuleWithProviders
type, wrapping the original component module and defining component routing under a specific parent route:
export class SomeComponentModule {
public static importFor(parentState: string): ModuleWithProviders {
return {
ngModule: SomeComponentModule,
providers: [
// What should I do here?
]
};
}
}
How can I implement this function effectively? How do I incorporate UIRuterModule.forChild(...)
within this function? These are the challenges I am currently faced with resolving.