I have multiple modules, each containing various components, and a parent component that needs to contain one of these components.
Since I cannot use routes to achieve this, I authored a service that given a moduleName :string
and a compomnentName : string
returns the corresponding component class.
See ComponentDirectoryService
in this stackblitz.
I am imitating how the router module allows feature modules to inject their own routes:
ComponentDirectoryModule
has aforRoot
andforChild
methods to which aDIRECTORY_ENTRIES
value is passed- each feature module (p1 and p2 in stackblitz) calls
forChild
to provide itsDIRECTORY_ENTRIES
- AppModule calls
forRoot
which providesComponentDirectoryService
passing in the cumulated (calls toforChild
) value ofDIRECTORY_ENTRIES
Everything works fine in dev mode.
In production mode everything works, but I get this scary warning:
Can't resolve all parameters for ComponentDirectoryService in ...component-directory.service.ts: (?). This will become an error in Angular v6.x
What puzzles me is that the only parameter to the service are the directory entries...
EDIT 1
Like Angular declares the ROUTES injection token:
export const ROUTES = new InjectionToken<Route[][]>('ROUTES');
so do I for DIRECTORY_ENTRIES:
export const DIRECTORY_ENTRIES = new InjectionToken<ComponentDirectoryEntry[][]>('DIRECTORY_ENTRIES');