Ionic-app-scripts: Ionic 3 is creating new instances of providers instead of single shared instance

Created on 25 Apr 2017  Â·  14Comments  Â·  Source: ionic-team/ionic-app-scripts

Short description of the problem:

Ionic 3 is creating new instances of providers instead of single shared instance.

What behavior are you expecting?

Expected behavior is a single shared provider is created. In Ionic 2, things are working as expected. Service is singleton. But in Ionic 3, which uses angular 4, it looks like every component is creating new instance of that class.

How to recreate:

  1. Define service as class :
@Injectable()
export class MyService {
 ...
}
  1. In other components or pages, import that class with just import command.
import { MyService } from '../pages/services/myservice';

In constructor:

constructor(public _MyService: MyService)
  1. In main app.module.ts, add that class service as provider:
providers: [someOtherThings, MyService, someOtherThings]

Which @ionic/app-scripts version are you using?
3.0.1

All 14 comments

I just used tried the above, within each page constructor; created a public instance of the service. this did not create a new instance of the service because it still had data on a property created on the apps initial start up.

singleton seems to be working for me

I'm having the same problem here. @donaldruby Is this a bug?

I think it might be related to providers being in a different module. But I'm not sure. I worked around the problem for now by using a non-provider singleton class.

Thanks,
Don

On May 11, 2017, at 11:47 PM, Wenli Zhang notifications@github.com wrote:

I'm having the same problem here. @donaldruby Is this a bug?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.

Sorry, actually I want to ask @danbucholtz if this is a bug, since I saw him assigned himself on this issue. Any progress here?

any update urgent

For my case, it is cased by const injector = ReflectiveInjector.resolveAndCreate([AService]); creating a new instance of AService. See detail here: https://forum.ionicframework.com/t/ionic-service-as-provider-is-creating-multiple-instances/90498/8?u=xianzheme

@Ovilia i don't understand, have you solved the issue

@theo4u Yes, by not using ReflectiveInjector.resolveAndCreate. Check if you have it or providers defined other than in app.module.ts.

@Ovilia just in the app.module.ts (providers:[]), and still not working

@theo4u same issue.. did you solve it ?

Not really , was unable to get the singleton to work out, just switched method, to using storage, then the service get method to retrieve

Same Issue here, cannot get singleton service to work like referenced in documentation

I know this is over 2 years old, but I'm having the same problem still on ionic version 4.12.0

`import { Injectable } from '@angular/core';

@Injectable({
providedIn: 'root'
})
export class MyService {`

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kensodemann picture kensodemann  Â·  108Comments

squallliu picture squallliu  Â·  62Comments

jgw96 picture jgw96  Â·  73Comments

kleeb picture kleeb  Â·  48Comments

shlomiassaf picture shlomiassaf  Â·  83Comments