vuex中action调用dispatch,失败

下面是我的actions.js中的方法

export const parseToken = ({dispatch}, token) => {
  return Vue.http
            .post(config.dev.host + '/api/parseToken', {
              token
            })
            .then((res) => {
              console.log(res)
              if (res.body.status == 0) {
                dispatch('SET_USERNAME', res.body.username)
              }
              return res
            }, (err) => {
              console.log(err)
            })
            .catch((err) => {
              console.log(`catch ${err}`)
            })
}

我是在main.js进行调用,已经引入store, parseToken

    parseToken(store.state, access_token)

下面是请求返回结果,res输出内容
图片描述

然后catch到了error

catch TypeError: dispatch is not a function

希望大家可以帮忙解决下。。。

阅读 10k
2 个回答

main.js中应该如此使用action


import store from 'your store file path';
import {parseToken} from 'you parseToken file path';
const App = Vue.extend({
    store,
    vuex:{
        action:{
            parseToken
        }
    },
    ready(){
        this.parseToken('xxx-xxx-xxx');
    }
});
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题