Answer the question
In order to leave comments, you need to log in
How to call another action in Vuex inside an action?
I'm making a catalog of cultural heritage objects in Vue.
Just a page with object cards, like a clothing store.
I'm using VueX to get data via an API and store it.
First, I think you need to clarify first.
I have state heritageObjects: [] where the objects are stored.
I also have four actions for working with the API:
1. GET_HERITAGEOBJECTS_FROM_DB
2. APPEND_HERITAGEOBJECT_TO_DB
3. REMOVE_HERITAGEOBJECT_FROM_DB
4. EDIT_HERITAGEOBJECT_IN_DB
I think it's clear what they all mean.
So, is it normal practice that, for example, after adding a new record to the database, I again make a request to get all records from the database?
Well, that is, inside APPEND_HERITAGEOBJECT_TO_DB I do this:
1. I make a POST request to add a new record to the database
2. I get all records from the database again using GET_HERITAGEOBJECTS_FROM_DB
Similarly, after REMOVE_HERITAGEOBJECT_FROM_DB and EDIT_HERITAGEOBJECT_IN_DB, I am going to make a request every time to get all records from DB.
Well, if this practice is normal, then the question is : how to use another action inside an action?
How to call GET_HERITAGEOBJECTS_FROM_DB inside APPEND_HERITAGEOBJECT_TO_DB?
GET_HERITAGEOBJECTS_FROM_DB({commit}) {
return axios('http://127.0.0.1:3000/test/1', {
method: "GET"
})
.then((heritageObjects) => {
commit('SET_HERITAGEOBJECTS_TO_STATE', heritageObjects.data);
return heritageObjects;
})
.catch((error) => {
console.log(error);
return error;
})
},
APPEND_HERITAGEOBJECT_TO_DB() {
return axios('http://127.0.0.1:3000/test/', {
method: "POST",
data: { name: "", description: "" }
})
.then((heritageObject) => {
return heritageObject;
})
.catch((error) => {
console.log(error);
return error;
})
// Вот так не работает почему-то:
// this.GET_HERITAGEOBJECTS_FROM_DB()
// Vue ругается: error Unreachable code no-unreachable
}
Answer the question
In order to leave comments, you need to log in
// Вот так не работает почему-то:
// this.GET_HERITAGEOBJECTS_FROM_DB()
this.dispatch('GET_HERITAGEOBJECTS_FROM_DB');
// Vue ругается: error Unreachable code no-unreachable
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question