Style Guide / Docs advice for partially shared lazy feature modules
See original GitHub issueπ Docs or angular.io bug report
Description
The docs / style guide should provide a guide / example for partially shared lazy feature modules.At the moment thereβs only the Sharing Modules guide for βcommonly used directives, pipes, and componentsβ. But there should be another guide that focuses on sharing βuncommenly used componentsβ. (Or: βlazy loaded modules that depend on some components of other lazy loaded modulesβ)
Example 1:
I have an enterprise application with lotβs of modules, which are all lazy loaded. But some of these modules use a few components and services of other modules.
app/
module1/
components/
M1Component1
M1Component2 <-- uses M1Component1 internally. no problem because it's the same module
module2/
M2Component1 <-- uses M1Component1 internally
Whatβs the best way to solve this? Putting these components into SharedModule
isnβt an option, because those components arenβt βcommonly usedβ. And importing the whole Module1
into Module2
isnβt an either, because I only need a few components from Module1
.
Example 2:
The application has a DashboardModule
which uses InjectionToken
and Provider
s to register WidgetComponent
s from other modules. Those Providers
must be registered at application start (or when the DashboardModule
gets loaded).
In other words: The DashboardModule
doesnβt know any widgets by itself. All widgets are provided by the other modules. But all those modules are lazy loaded.
app/
dashboard/
services/
DashboardWidgetTypeService <-- with @Inject(DASHBOARD_WIDGET) widgets
module1/ <-- provides M1DashboardComponent as DASHBOARD_WIDGET
components/
M1DashboardComponent
module2/ <-- provides M2DashboardComponent as DASHBOARD_WIDGET
components/
M2DashboardComponent
Current Solution:
At the moment each module consists of 2 modules: a SharedFeatureModule
and a FeatureModule
. And the AppModule
imports all those SharedFeatureModule
s.
app/
module1/
MyFeature1Module <-- imports MyFeature1RoutingModule and MyFeature1SharedModule
MyFeature1RoutingModule
MyFeature1SharedModule <-- has forRoot method to register Dashboard providers
module2/
MyFeature2Module <-- imports MyFeature2RoutingModule and MyFeature2SharedModule and MyFeature1SharedModule
MyFeature2RoutingModule
MyFeature2SharedModule <-- has forRoot method to register Dashboard providers
AppModule <-- imports MyFeature1SharedModule.forRoot() and MyFeature2SharedModule.forRoot()
The AppModule
imports all SharedFeatureModule
s via forRoot()
to register the dashboard widget providers.
Iβm not yet sure whatβs the βbest practiceβ for this. But Iβm sure youβll have some good advice.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:7 (4 by maintainers)
This question has been asked in many place and no good answer had been provided yet. This clearly missing from docs. Linking a stackoverflow question (for google to point people here once answer is available) https://stackoverflow.com/questions/39648119/importing-a-component-from-a-lazy-loaded-module-into-another-lazy-loaded-module
The question is suited for github, because it should result in an improvement for the docs.
For the second part it doesnβt really matter if itβs an InjectionToken or if I call a Service. The point is: The registration must be done when the application starts. But the registration is also part of a (lazy) feature module and uses a few components from that (lazy) module.
This means:
AppModule
must import the βshared partβ of the feature module, because thereβs no other way to eagerly register providers (or call a service) when the feature module is lazy loaded. The βshared partβ includes the registration process (injection token or service call) of the widget, plus the widget component, and all other needed components for the widget.