为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

杨__羊羊 2018-08-31 21:19:42
页面代码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)     } }
查看完整描述

2 回答

?
料青山看我应如是

TA贡献1772条经验 获得超7个赞

export const addList = (commit) => (item) => {
  commit(types.ADD_LIST, item)
 }

没用过vuex,不过如果和Redux差不多的话,应该是这么用。


查看完整回答
反对 回复 2018-09-09
?
一只萌萌小番薯

TA贡献1795条经验 获得超7个赞

actions.js文件应该改成下面这样

export const addList = ({commit}, item) => {
  commit('ADD_LIST', item)
}    
export const delList = ({commit}, item) => {
  commit('DELETE_LIST', item)
}

或者还是用之前的写法,但是要新建一个mutation-types.js文件

export const ADD_LIST = 'ADD_LIST'
export const DELETE_LIST = 'DELETE_LIST'

actions.js

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


查看完整回答
反对 回复 2018-09-09
  • 2 回答
  • 0 关注
  • 4707 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信