javascript - vuex到底如何在页面调用actions?

查看:341
本文介绍了javascript - vuex到底如何在页面调用actions?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

页面代码

methods: {
    ...mapActions(['addList', 'delList']),
    add (value) {
      this.addList({title: value})
    },
    del (item) {
      this.delList(item)
    }
}

actions中代码

import * as types from './mutations'
export const addList = ({commit}, item) => {
  commit(types.ADD_LIST, item)
}    
export const delList = ({commit}, item) => {
  commit(types.DELETE_LIST, item)
}

但是执行会报[vuex] Expects string as the type, but found function.求高手指点一下哪里出错了,非常感谢.
如果把上面代码改为下面的代码就可以执行

methods: {
    add (value) {
      this.$store.commit('ADD_LIST', {title: value})
    },
    del (item) {
      this.$store.commit('DELETE_LIST', item)
    }
}

解决方案

methods: {
    add (value) {
      this.$store.commit('ADD_LIST', {title: value})
    },
    del (item) {
      this.$store.commit('DELETE_LIST', item)
    }
}

问一下,你说这段代码可以执行达到效果,那为什么不是this.$store.commit(types.DELETE_LIST, item)?

你actions.js中都用了types,是不是mutation-types.js文件出了问题呢?

这篇关于javascript - vuex到底如何在页面调用actions?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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