Search code examples
vue.jsvuejs2vue-componentvuexvuex-modules

How can I get response ajax by vuex store in the vue component?


My component vue like this :

<template>
    ...
</template>
<script>
    import {mapActions, mapGetters} from 'vuex'
    export default {
        ...
        methods: {
            add(event) {
                this.addProduct(this.filters)
                console.log(this.getStatusAddProduct)
                if(this.getStatusAddProduct) {
                    ...
                }
            },
            ...mapActions(['addProduct'])
        },
        computed: {
            ...mapGetters(['getStatusAddProduct'])
        }
    }
</script>

This code : this.addProduct(this.filters), it will call addProduct method n the modules vuex

My modules vuex like this :

import { set } from 'vue'
import product from '../../api/product'
import * as types from '../mutation-types'
// initial state
const state = {
    statusAddProduct: null
}
// getters
const getters = {
    getStatusAddProduct: state =>  state.statusAddProduct
}
// actions
const actions = {
    addProduct ({ dispatch, commit, state }, payload)
    {
        product.add(payload,
            data => {
                commit(types.ADD_PRODUCT_SUCCESS)
            },
            errors => {
                commit(types.ADD_PRODUCT_FAILURE)
            }
        }
    }
}
// mutations
const mutations = {
    [types.ADD_PRODUCT_SUCCESS] (state){
        state.statusAddProduct = true
    },

    [types.ADD_PRODUCT_FAILURE] (state){
        state.statusAddProduct = false
    }
}
export default {
    state,
    getters,
    actions,
    mutations
}

This code : product.add(payload, in the modules vuex, it will call api

The api like this :

import Vue from 'vue'
export default {
    add (filter, cb, ecb = null ) {
        axios.post('/member/product/store', filter)
            .then(response => cb(response))
            .catch(error => ecb(error))
    }
}

My problem here is if add method in vue component run, the result of console.log(this.getStatusAddProduct) is null. Should if product success added, the result of console.log(this.getStatusAddProduct) is true

I think this happens because at the time of run console.log(this.getStatusAddProduct), the process of add product in vuex modules not yet finished. So the result is null

How can I make console.log(this.getStatusAddProduct) run when the process in the vuex module has been completed?


Solution

  • You have to pass the property down all the way from the .add() method.

    You do that by returning it in the intermediary methods and, lastly, using .then().

    The api:

    add (filter, cb, ecb = null ) {
        return axios.post('/member/product/store', filter) // added return
            .then(response => cb(response))
            .catch(error => ecb(error))
    }
    

    And, the action:

    addProduct ({ dispatch, commit, state }, payload) // added return
    {
        return product.add(payload,
            data => {
                commit(types.ADD_PRODUCT_SUCCESS)
            },
            errors => {
                commit(types.ADD_PRODUCT_FAILURE)
            }
        }
    }
    

    Finally:

    methods: {
        add(event) {
            this.addProduct(this.filters).then(() => { // added then
                console.log(this.getStatusAddProduct)  // moved inside then
                if(this.getStatusAddProduct) {
                    ...
                }
            })
        },