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.

Angular singleton service initiated multiple times

See original GitHub issue

Hi everyone, I was following this tutorial , then i tried to share a library between the shell and the mfe1 app. I created the lib outside the workspace directory with one service injected in root, then added it to the tutorial package.json, and imported it in both shell and mfe1. Inside both projects webpack config, i added the following configuration

new ModuleFederationPlugin({
      .....
        },        
        shared: {
          .....
          "my-lib": { singleton: true, strictVersion: true, requiredVersion: '1.0.0'},
          ...sharedMappings.getDescriptors()
        }
        
    }),

When testing the shell, i noticed that the singleton service is being initiated twice, first time when loading the shell, second time when moving to the mfe1, maybe i’m missing something, but isn’t the purpose of singleton: true config is to insure that only one instance of that service is existing?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
careywalkercommented, Jul 8, 2021

The team here found the issue and reading the Angular docs on providers: https://angular.io/guide/providers I can see what my issue was. We are lazy loading the modules and this was the cause of the multiple instances of the service being created.

I updated providedIn to be the containing module instead of ‘root’

@Injectable({
  providedIn: SharedServicesModule
})
1reaction
ScriptedAlchemycommented, Oct 1, 2022

That’ll do it. ShareAll should use webpacks resolvers. They can follow symlinks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular Service singleton constructor called multiple times
I am trying to use an app-wide service (UserService) that stores authenticated user details. I have set ...
Read more >
How to avoid Angular injectable instances duplication
In this article we'll explore when and why Angular creates two instances of the same service and what solutions exist to ensure a...
Read more >
Singleton services
A singleton service is a service for which only one instance exists in an application. For a sample application using the app-wide singleton...
Read more >
A singleton service in Angular
A singleton service in angular is a service for which there exists only one instance in an application. What are ways to create...
Read more >
[Solved]-Singleton service in Angular 2-angular.js
In Angular2 services are singletons per provider. If you provide a service multiple times, you'll get multiple instances. If you provide it in...
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