A
A
alzow2018-09-04 17:50:07
Vue.js
alzow, 2018-09-04 17:50:07

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 => {
                // Что-то пошло не так. Надо уведомить пользователя об этом. 
           });
    },
    }
})

Something tells me that showing notifications from the store is a bad idea, and you should return a promise, but then you will have to process the first and second cases from the place where the action was called from, which will complicate everything

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alvvi, 2018-09-04
@alvvi

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 question

Ask a Question

731 491 924 answers to any question