search

Home  >  Q&A  >  body text

javascript - Regarding vuex, what is known is that the dispatch method is called in the component, and the return value is undefined. I don’t know what went wrong.

Using vuexmodules

export const GET_TABGRADE = 'GET_TABGRADE'
    const tanjie = axios.create({
        baseURL: 'tanjie'
    })
    
    findGrade1: function () {
        return new Promise((resovle, reject) => {
            tanjie({
                url: '/content/findGrade1'
            }).then(response => {
                console.log('api is transfer')
                resovle(response.data)
            }).catch(error => {
                reject(error)
            })
        })
    },
import baseApi from '@/api/base'
import * as types from '../mutation-type'

const state = {
    tabGrade: {
        errno: 1,
        list: {}
    }
}

const getters = {
    getTabGrade: state => {
        state.tabGrade
    }
}

const actions = {
    // 调用api
    getTabGrade({ commit }) {
        console.log('is actions')
        return new Promise(function (resolve, reject) {
            baseApi.findGrade1()
                .then(res => {
                    commit(types.GET_TABGRADE, res)
                    resolve(res);   
                }).catch(err => {
                    console.log(err)
                })
        })

    }
}

const mutations = {
    [types.GET_TABGRADE](state, res) {
        
        state.tabGrade = {
            ...state.tabGrade,
            list: res.list
        }
        console.log(state.tabGrade)
        
    }
}

export default {
    state,
    getters,
    actions,
    mutations
}
  computed: {
    ...mapGetters([
      'getTabGrade'
    ]),
    
  created() {
    this.$store.dispatch('getTabGrade')
      .then(res => {
        console.log(res) // undefined
        return res
      })
      .catch(err => {
        console.log(err)
      })
  },

There should be no problem with the references to each module. After all, the component can be accessed through this.$store state

But I don’t know why dispatch returns undefined

The correct data can be obtained in the component. How can I use it, like displaying a grade1_name through {{}}

我想大声告诉你我想大声告诉你2747 days ago1704

reply all(2)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-07-05 10:43:22

    Added return and resolve, otherwise the call triggered by this.$store.dispatch('getTabGrade') may not return a promise with the correct value. Please try it.

    const actions = {
        // 调用api
        getTabGrade({ commit }) {
            console.log('actions')
             return baseApi.findGrade1()    //添加return
                .then(res => {
                    commit(types.GET_TABGRADE, res)
                    resolve(res);   //添加resolve
                }).catch(err => {
                    console.log(err)
                })
        }
    }
    
    

    reply
    0
  • 怪我咯

    怪我咯2017-07-05 10:43:22

    Solved, I finally did some processing in the getter of the module:

    const getters = {
        getTabGrade: state => {
            console.log('getter',state.tabGrade)
            let tabGradeName = []
            for(var i =0; i<state.tabGrade.list.length; i++){
                tabGradeName.push(state.tabGrade.list[i].grade1_name)
            }
            return tabGradeName
        }
    }

    The calculated properties are written in the final component using template syntax,
    html:

    <span>{{getTabGrade[0]}}</span>
    
    

    js:

      computed: {
        ...mapGetters([
          'getTabGrade'
        ])

    reply
    0
  • Cancelreply