Answer the question
In order to leave comments, you need to log in
Vuex how to properly fetch data via REST API?
Good afternoon, such a question, Google did not find anything sensible.
There is a
List of elements
I get the whole list into action through axios and cause a mutation that sets the state of lists.[ {id: 12, name: "12"}, {id: 43, name: "43"}]
const actions = {
getList({commit}){
let list = api.getList();
commit('setList', list);
}
}
const mutations = {
setList(state, list){
state.list = list;
}
}
created () {
this.$store.dispatch('getList');
},
computed: {
list(){
return this.$store.state.list;
}
},
const getters = {
getItemById: state => id => {
return api.getItem(id);
}
}
Answer the question
In order to leave comments, you need to log in
1. I don't see anything incorrect in this approach.
2. It is better to change item through mutation/action. Use Vue.set otherwise Vue won't track changes to the parent list.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question