question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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 Providers to register WidgetComponents 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 SharedFeatureModules.

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 SharedFeatureModules 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
yelhouticommented, Oct 2, 2019

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

1reaction
benneqcommented, Jun 6, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lazy-loading feature modules - Angular
Setting up a lazy-loaded feature module requires two main steps: Create the feature module with the Angular CLI, using the --route flag. Configure...
Read more >
Lazy-load | Contributed modules - Drupal
Lazy -load gives the user ability to enable lazy-loading the images on their Drupal 7 and 8 sites. This guide focuses only on...
Read more >
The Recommended Setup - Single SPA
The single-spa core team recommends choosing either import maps or module federation for your shared, third-party dependencies. We do not recommend sharing someΒ ......
Read more >
Angular documentation style guide
This style guide is for anyone who contributes to the Angular documentation (this site). These guidelines should be followed by all authors.
Read more >
Angular Lazy Load Common Styles Specific to a Feature Module
Here I will walk you through a step-by-step guide on how to lazy load the common styles specific to a feature module.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found