頁面程式碼
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)
}
}
問一下,你說這段程式碼可以執行達到效果,那為什麼不是
this.$store.commit(types.DELETE_LIST, item)
?你actions.js中都用了types,是不是
mutation-types.js
檔案出了問題呢?沒用過vuex,不過如果跟Redux差不多的話,應該是這麼用。
找到問題的答案了,感謝樓上的提醒,actions.js檔案應該改成下面這樣
或還是用之前的寫法,但要新建一個mutation-types.js檔
actions.js