vue.js - vuex中action调用dispatch,失败

查看:1351
本文介绍了vue.js - 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

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

解决方案

dispatch方法在stroe上,而不是在store.state上

改成

parseToken(store, access_token)

https://jsfiddle.net/ycloud/6...

这篇关于vue.js - vuex中action调用dispatch,失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆