I am trying to implement a Singleton in my class, but somehow this
does not exist when I call the funcA
function. One of my first calls in my Software is a call to the funcB
function. Here everything works fine, I receive the content I intended to get. Later I try to call it from a different class again, but this time the funcA
function. Now I get the Error I wrote down below. Why does it work when I call it in one file but not in a different?
export class Singelton {
private static _instance: Singelton;
private _service: Service;
private obj: Object;
private constructor() {
this.obj = null;
this._service = new Service();
}
public static getInstance(): Singelton {
if (!Singelton._instance) {
Singelton._instance = new Singelton();
}
return Singelton._instance;
}
public funcA(data: Data): void {
this._service.someLogic(...)
}
public funcB(): Object | null {
return this.obj;
}
}
This is the Error message:
ERROR TypeError: Cannot read properties of undefined (reading '_service') (from funcA)