Answer the question
In order to leave comments, you need to log in
Vuex: Why is it returning an empty object?
I use vuex to store, retrieve and return data to components. Got $store, got data.
The Vuex documentation states that it is desirable to use getters to get data.
const store = new Vuex.Store({
state: {
news: [],
},
getters: {
news: (state) => state.news
},
...
});
export default {
...
computed: {
news () {
console.log(this);
console.log(this.$store.getters.news);
return this.$store.getters.news;
}
},
...
}
Answer the question
In order to leave comments, you need to log in
Why make a similar getter at all, which returns just a state from the store?
If the same you can get through:
...mapState({
news: state => state.news,
});
this.$store.state.news
const actions = {
async fetchNews({ commit }) {
// Стреляете запрос в нужный эндпоинт
const { data } = await axios.get('http://your.api.endpoint');
if (data.result) {
// Записываете новости в стору с помощью мутации
commit(SET_NEWS, data.data);
} else {
// Обрабатываете ошибку, если не удалось получить новости
}
return data;
},
};
v-if="news.length > 0"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question