Reputation: 1622
I'm working on ionic 3 and Angular 4 with typescript and I'm confused on what needs to be done.
Let me first present what I have:
I have a class that has a private static _instance: ClassName
property.
This is the constructor: private constructor(private platform: Platform, private file: File){}
and then I have a static get instance property
static get instance() {
if(!ClassName._instance){
ClassName._instance = new ClassName();
}
return ClassName._instance;
}
now my question is, the new-ing is not allowed inside the get instance() because of Platform and File service being injected in the class's constructor. How do i go on about creating a new instance or solve this problem? What could be a possible scenario if I make my injectors optional?
constructor(private platform?: Platform, private file?: File){}
Upvotes: 1
Views: 883
Reputation: 1622
If anybody stumbles across this question then you can make your injectable singleton by just declaring it inside providers array in app.module.ts. That maintains a single instance across the app. Any time you have lazy loaded modules and declare your injectable inside the providers of that lazily loaded module, you are creating multiple instances of the injectable.
Upvotes: 0