页面代码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条经验 获得超8个赞
export const addList = (commit) => (item) => { commit(types.ADD_LIST, item) }
没用过vuex,不过如果和Redux差不多的话,应该是这么用。
一只萌萌小番薯
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) }
添加回答
举报
0/150
提交
取消