-
Notifications
You must be signed in to change notification settings - Fork 25.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dependency injection creates new instance each time if used in router submodule #12889
Comments
Sounds like #12869 |
I don't think its the same. In my case object is created each time I navigate. |
@peku33 loadChildren creates a new DI context for services. If you want to share services across modules you'll need to put those into a shared module and import them into the root module using the forRoot static method. new: shared.module.ts
root.module.ts
|
@guojenman I know. But the problem is, that DI creates multiple objects for components inside THE SAME module |
We have the same issue at this moment. I've edited the plunker from the module implementation example, the constructor of the crisis service now display's an alert when constructed:
|
We are also seeing the same issue when using services for inter-component communication in lazy-loaded modules. It is forcing us to stick with version 2.1.2 for the time being. I have checked with version 2.2.1 (released today) and can confirm that the issue still exists. |
I have a similar issue at the moment (using angular 2.2.1). While my services (all declared as providers in the root module) are instantiated only once , all of my components a newly created when I route to them. |
I have the exact same issue my services keep getting recreated in the components even though the only time the service is in the providers array is in the root sub-module. Running 2.2.1 |
Same problem here. Everything worked flawlessly with Angular 2.1.2 and Router 3.1.2. After upgrading to Angular 2.2.3 and Router 3.2.3 -> boom! When I added a constructor() to the module, I could confirm that even the whole module was instantiated multiple times, and not just the provider declared within that module. The module was activated lazily with a:
inside an app.routing.ts, which is included in an |
Can also verify that 2.1.2 removes the issue of services within a module being instantiated every time a route changes. 2.2.x and 2.3.0 both have this issue so it is not resolved yet. |
fixes angular#12869 fixes angular#12889 fixes angular#13885 fixes angular#13870 Before this change there was a single injector tree. Now we have 2 injector trees, one for the modules and one for the components. This fixes lazy loading modules. See the design docs for details: https://docs.google.com/document/d/1OEUIwc-s69l1o97K0wBd_-Lth5BBxir1KuCRWklTlI4 BREAKING CHANGES `ComponentFactory.create()` takes an extra optional `NgModuleRef` parameter. No change should be required in user code as the correct module will be used when none is provided DEPRECATIONS The following methods were used internally and are no more required: - `RouterOutlet.locationFactoryResolver` - `RouterOutlet.locationInjector`
fixes angular#12869 fixes angular#12889 fixes angular#13885 fixes angular#13870 Before this change there was a single injector tree. Now we have 2 injector trees, one for the modules and one for the components. This fixes lazy loading modules. See the design docs for details: https://docs.google.com/document/d/1OEUIwc-s69l1o97K0wBd_-Lth5BBxir1KuCRWklTlI4 BREAKING CHANGES `ComponentFactory.create()` takes an extra optional `NgModuleRef` parameter. No change should be required in user code as the correct module will be used when none is provided DEPRECATIONS The following methods were used internally and are no more required: - `RouterOutlet.locationFactoryResolver` - `RouterOutlet.locationInjector`
fixes angular#12869 fixes angular#12889 fixes angular#13885 fixes angular#13870 Before this change there was a single injector tree. Now we have 2 injector trees, one for the modules and one for the components. This fixes lazy loading modules. See the design docs for details: https://docs.google.com/document/d/1OEUIwc-s69l1o97K0wBd_-Lth5BBxir1KuCRWklTlI4 BREAKING CHANGES `ComponentFactory.create()` takes an extra optional `NgModuleRef` parameter. No change should be required in user code as the correct module will be used when none is provided DEPRECATIONS The following methods were used internally and are no more required: - `RouterOutlet.locationFactoryResolver` - `RouterOutlet.locationInjector`
fixes angular#12869 fixes angular#12889 fixes angular#13885 fixes angular#13870 Before this change there was a single injector tree. Now we have 2 injector trees, one for the modules and one for the components. This fixes lazy loading modules. See the design docs for details: https://docs.google.com/document/d/1OEUIwc-s69l1o97K0wBd_-Lth5BBxir1KuCRWklTlI4 BREAKING CHANGES `ComponentFactory.create()` takes an extra optional `NgModuleRef` parameter. No change should be required in user code as the correct module will be used when none is provided DEPRECATIONS The following methods were used internally and are no more required: - `RouterOutlet.locationFactoryResolver` - `RouterOutlet.locationInjector`
fixes angular#12869 fixes angular#12889 fixes angular#13885 fixes angular#13870 Before this change there was a single injector tree. Now we have 2 injector trees, one for the modules and one for the components. This fixes lazy loading modules. See the design docs for details: https://docs.google.com/document/d/1OEUIwc-s69l1o97K0wBd_-Lth5BBxir1KuCRWklTlI4 BREAKING CHANGES `ComponentFactory.create()` takes an extra optional `NgModuleRef` parameter. No change should be required in user code as the correct module will be used when none is provided DEPRECATIONS The following methods were used internally and are no more required: - `RouterOutlet.locationFactoryResolver` - `RouterOutlet.locationInjector`
fixes angular#12869 fixes angular#12889 fixes angular#13885 fixes angular#13870 Before this change there was a single injector tree. Now we have 2 injector trees, one for the modules and one for the components. This fixes lazy loading modules. See the design docs for details: https://docs.google.com/document/d/1OEUIwc-s69l1o97K0wBd_-Lth5BBxir1KuCRWklTlI4 BREAKING CHANGES `ComponentFactory.create()` takes an extra optional `NgModuleRef` parameter. No change should be required in user code as the correct module will be used when none is provided DEPRECATIONS The following methods were used internally and are no more required: - `RouterOutlet.locationFactoryResolver` - `RouterOutlet.locationInjector`
I am experiencing this issue on 4.2.4. |
Hello, does anyone have any option/way to solve this? |
fixes angular#12869 fixes angular#12889 fixes angular#13885 fixes angular#13870 Before this change there was a single injector tree. Now we have 2 injector trees, one for the modules and one for the components. This fixes lazy loading modules. See the design docs for details: https://docs.google.com/document/d/1OEUIwc-s69l1o97K0wBd_-Lth5BBxir1KuCRWklTlI4 BREAKING CHANGES `ComponentFactory.create()` takes an extra optional `NgModuleRef` parameter. No change should be required in user code as the correct module will be used when none is provided DEPRECATIONS The following methods were used internally and are no more required: - `RouterOutlet.locationFactoryResolver` - `RouterOutlet.locationInjector`
fixes angular#12869 fixes angular#12889 fixes angular#13885 fixes angular#13870 Before this change there was a single injector tree. Now we have 2 injector trees, one for the modules and one for the components. This fixes lazy loading modules. See the design docs for details: https://docs.google.com/document/d/1OEUIwc-s69l1o97K0wBd_-Lth5BBxir1KuCRWklTlI4 BREAKING CHANGES `ComponentFactory.create()` takes an extra optional `NgModuleRef` parameter. No change should be required in user code as the correct module will be used when none is provided DEPRECATIONS The following methods were used internally and are no more required: - `RouterOutlet.locationFactoryResolver` - `RouterOutlet.locationInjector`
For those still experiencing this issue after it has been fixed: Watch out for side-effects of route configuration that may make it seem like a new service instance is being injected for different routes. For example, Desktop Chrome and Mobile Safari have different outcomes for the following: Template:
Component:
In mobile safari, a hard reload happens on the save router-navigate (because the button type defaults to 'submit'). In Desktop Chrome it operates as a refreshless hash change. A hard reload obviously creates a new service instance. |
I'm experiencing this issue as well. Is it still a present bug or is the problem on my end? Angular 5.1.3 |
I'm experiencing this issue as well on Angular 5 |
Same here, although the configuration is a bit different.
The ParentComponent should be the same instance but i gets re-instantiated upon navigation between
|
@jbgraug ... just a question ... where did you get your routes syntax above ... especially mentioning |
It is just an example, my application is more complicated than that.
@mlc-mlapis Not sure if i understood your question... By the way i'm using Angular ^5.2.0 |
@jbgraug ... because it should be something like ... this level could be a child for any parent of course ...
where routing for
|
@mlc-mlapis , It is not the same. Think of some Master/detail where navigation should render one detail module or the other (lazily) |
@jbgraug ... still not understand to which Or you mean that BTW ... |
Thats exactly the case. |
@jbgraug ... hmm, so you are describing a new functionality ... and then it is necessary to ...
|
@mlc-mlapis I've achieved the desired behaviour (only one parent instance) by changing the structure to this:
I guess the doc could be improved in https://angular.io/guide/router |
Why is this closed? |
Same issue here. I just modified the angular documentation example on singleton by just adding an alert: Click on the Customer route to see the constructor being called. |
@sijucm I'm facing exactly same issue for Lazy loaded modules. Shared providers are instantiated twice though we have followed 'forRoot' convention at the root module. Folks, Any updates on this issue? This issue still exist in 5.2.11 version I really appreciate a quick response from the team. |
workaround for this issue:
I reallly hate this to do in the project :( but no chance |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
I'm submitting a ... (check one with "x")
Current behavior
My application is divided into modules. Router navigates between modules, each child modules has some providers and some components. I expect application to reuse services injected to components within one module. However, when I navigate, service constructor is called each time component is loaded.
RootModule:
SubModule:
Each time I navigate between /module/test1 and /module/test2 - TestService constructor is called
Expected behavior
If test1 and test2 are defined as routes in RootModule, only one instance of TestService is created. I expect SubModule to inject the same instance of TestService if I navigate between its components.
If I move TestService provider to RootModule - works as expected
Minimal reproduction of the problem with instructions
https://github.com/peku33/angular2-dependency-injection-failure
What is the motivation / use case for changing the behavior?
Most of components of one of modules uses a service that opens and maintains WebSocket connection. Each time a service is spawned, new connection is opened. I definitely want to reuse the component and keep single connection open than close and reopen it every time user navigates between pages.
Please tell us about your environment:
Everything is up to date, browser - Chrome
Angular version: 2.0.X
Have 'latest' for almost everything in package.json
@angular/core@2.2.0
Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]
Language: [all | TypeScript X.X | ES6/7 | ES5]
Node (for AoT issues):
node --version
=The text was updated successfully, but these errors were encountered: