Singleton Service constructor getting called multiple times in a nativescript-schematics code sharing project. Here is some important dependencies version from my package.json:
"tns-android": {"version": "5.0.0"}
"@angular/core": "~7.1.0",
"nativescript-angular": "^7.1.0",
"tns-core-modules": "^5.0.5",
"@nativescript/schematics": "^0.4.0",
"nativescript-dev-typescript": "^0.7.8",
"nativescript-dev-webpack": "^0.17.0",
"typescript": "~3.1.1"
I have tried providedIn: 'root'
described in angular official docs and checking the singletonInstance as well. Constructor is getting called multiple times.
@Injectable({ providedIn: 'root'})
export class UserService {
constructor(private _http: HttpClient) {
if (!UserService.singletonInstance) {
console.log('in user service constructor');
UserService.singletonInstance = this;
} else {
return UserService.singletonInstance;
}
}
Do I need to use forRoot as we have app.module.ts and app.module.tns.ts ?
After doing some further investigation I found that Visual code intellisense has imported the service from .js file instead of .ts file. Hence it was making the multiple constructor calls. I am adding that as it may be helpful for someone’s debugging.