I am currently learning vue.js and struggling with the concept of promises. I need to initialize a variable with data from an API call, but I want to ensure that the Axios call is generic:
{
data: {
list: [],
},
methods: {
ShowList: function () {
return this.Axios_GET('/api/Api_Store').then(items => {
this.list = items;
});
},
Axios_GET: function (apiurl) {
// I want this method to be reusable without binding variables inside it
this.StartProgress();
axios({ method: 'get', url: apiurl }).then((response) => {
this.StopProgress();
return Response.data;
}).catch((error) => {
this.StopProgress();
}).then(function () {
});
},
}
};
However, when I attempt to use ShowList
, I encounter the following error:
Error in mounted hook: "TypeError: Cannot read property 'then' of undefined"
I wish to write the ShowList function to retrieve data from the API like this (in theory)
this.list = this.Axios_GET('/api/Api_Store')
Note: The functions StartProgress
and StopProgress
are already defined and operational.