Angular singleton service initiated multiple times
See original GitHub issueHi 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:
- Created 2 years ago
- Comments:11 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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’
That’ll do it. ShareAll should use webpacks resolvers. They can follow symlinks