[Vuejs]-Get vuex action response object in component context

1👍

Return promise to get the response in the component

list({ commit }, payload)
    {
        commit( 'Loader/SET_LOADER', { status:1 }, { root: true });
        return new Promise((resolve,reject)=>{
          axios.get('/api/posts', { params: payload })
          .then((response) => {
              commit('Loader/SET_LOADER', { status:2, response:response }, { root: true });  
              console.log(response);
              commit('SET_POSTS',  response.data.data.data );
              commit('SET_PAGINATION',  response.data.pagination );
              resolve(response);
          })
          .catch((error) => {
              commit('Loader/SET_LOADER', { status:3, errors: error }, { root: true });           reject(error);
              throw error;
          });
       })
    },

Using async

async list({commit}, payload) {
    commit('Loader/SET_LOADER', {status: 1}, {root: true});
    try {
        let response = await axios.get('/api/posts', {params: payload});
        commit('Loader/SET_LOADER', {status: 2, response: response}, {root: true});
        console.log(response);
        commit('SET_POSTS', response.data.data.data);
        commit('SET_PAGINATION', response.data.pagination);
        return response;
    } catch (e) {
        commit('Loader/SET_LOADER', {status: 3, errors: error}, {root: true});
        throw error;
    }
}
👤Rijosh

Leave a comment