Answer the question
In order to leave comments, you need to log in
How to show notifications from Vuex?
I am developing a small VueJS application for educational purposes and faced the question of how to show notifications to the user (all sorts of pop-up messages) when the API responds from the server.
Code for clarity:
export default new Vuex.Store({
state,
getters,
mutations,
actions: {
load_current_user (context, {}) {
Vue.http.get(`/api/users/me/`).then(response => {
context.commit('set_current_user', response.body);
}, response => {
// Что-то пошло не так. Надо уведомить пользователя об этом.
});
},
}
})
Answer the question
In order to leave comments, you need to log in
So what notifications do you want? At the application level or at the level of a single component? If at the application level, then naturally the logic should be tied to the store. If at the component level, return a promise.
I don’t know about vue-resources, there are no problems with normal promises.
export default new Vuex.Store({
actions: {
load_current_user (context, {}) {
// Помните что .then возвращает именно промис
return axios.get('/api/users/me/')
.then(response => {
context.commit('set_current_user', response.body)
})
},
}
})
// В компоненте
this.$store.dispatch('load_current_user', {}).catch(e => {
// Что-то пошло не так. Надо уведомить пользователя об этом.
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question