Answer the question
In order to leave comments, you need to log in
How to correctly call methods for API requests?
There is 1 option
methods: {
...mapActions('module name', ['getData'])
},
mounted() {
this.getData();
},
actions: {
async getData({ commit }) {
try {
const response = await axios.get(url);
commit('mutation name', response.data);
} catch (e) {
console.error(e);
}
},
},
methods: {
...mapActions('module name', ['getData'])
},
mounted() {
this.getData().then((res) => {
if(res.status !== 200) {
console.log('Error')
}
});
},
actions: {
async getData({ commit }) {
try {
const response = await axios.get(url);
commit('mutation name', response.data);
return response;
} catch (e) {
console.error(e);
return null;
}
},
},
axios.get(url)
.then()
.catch(e)
.finally()
async getData({ commit }) {
return await axios.get(url).then((res) => {
commit('mutation name', res.data)
}).catch(e => console.error(e));
}
},
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question