0👍
✅
Just as @skirtle commented i needed to move my function to actions because i’m using a async task.
My final code ended up that way:
getters: {
items: state => state.items,
...
}
mutations: {
'LOAD_ITEMS'(state, data) {
const items = []
for (let key in data) {
const item = data[key];
item.id = key;
items.push(item);
}
state.items = items;
...
}
actions: {
loadItems({ commit, state }) {
...
return axios.get("item.json")
.then(response => {
commit("LOAD_ITEMS", response.data);
}).catch(error => console.log(error));
}
}
I’m using firebase, so i did a loop to store the key.
If you has your own API maybe you can just store response.data directly in a JS object without using a loop.
Source:stackexchange.com