I want to loop over an array of objects using v-for. However my state returns an object containing an array of Objects:
{ "todos": [ { "id": "x", "description": "y" }, { "id": "a", "description": "b" } ] }
which causes v-for not to work. If I use a getter instead it returns my array of objects like it's supposed to:
[ { "id": "x", "description": "y" }, { "id": "a", "description": "b" } ]
is this normal behaviour?
using mapState:
<template>
<div class="content-center">
{{todos}}
</div>
</template>
....
import {mapActions, mapGetters, mapState} from 'vuex';
export default {
....
computed: mapState(['todos'])
}
using getter:
<template>
<div class="content-center">
{{getTodos}}
</div>
</template>
....
import {mapActions, mapGetters, mapState} from 'vuex';
export default {
....
computed: mapGetters(['getTodos'])
}
I also wanna add that i'm using modules, in case that changes anything about calling the mapState
vuex:
const state = {
todos: []
};
const getters = {
getTodos: state => state.todos
};
const actions = {
loadTodos({commit}) {
axios.get('/api/todos', {
}).then(response => {
commit('setTodos', response.data);
}).catch(error => {
console.error(error);
})
}
};
const mutations = {
setTodos: (state, response) => {
state.todos = response;
}
};