I am attempting to utilize a class with a constructor object inside another class. How should I properly invoke this class? For example, how can I use Class 1 within Class 2?
Below is an instance where an object is being created from a response obtained from an axios call. The _object
should represent the data retrieved from getData()
. I am uncertain if this approach is correct. In what way can I then reference this class with the constructor in another class? My goal is to be able to access the properties of the object and utilize them in the other class, but I am unsure of the correct method to call the class.
Class 1:
export class MyClass {
private _object: any;
constructor(object: any) {
this._object = object;
}
public static async getData() {
return axios.get(url)
.then(response => response.data)
.catch((error) => {
console.log(error);
});
}
public static async getCurrentData() {
return new MyClass(await this.getData());
}
}
Attempting to implement this in another class with a constructor object:
Class 2:
new MyClass(object); // cannot find name object
or
new MyClass(); // without constuctor object, Expected 1 arguments, but got 0. An argument for 'object' was not provided.'
I have posted a related question here: return properties of constructor object