报错信息:
vue.runtime.esm.js?ff9b:1888 Error: [vuex] do not mutate vuex store state outside mutation handlers.
[Vue warn]: Error in callback for watcher "function () { return this._data.$$state }": "Error: [vuex] do not mutate vuex store state outside mutation handlers."
(found in <Root>)
报错原因:
使用vuex时,在mutation外部修改了state中的数据,就会报这个错误。
解决办法:
1、我的是在actions中修改了state中的数据,只需要将数组、对象等数据拷贝一个副本就行了。数字类型不用做此操作。
let playList = state.playList.slice()
let sequenceList = state.sequenceList.slice()
let currentIndex = state.currentIndex
2、关闭严格模式就可以了: strict: false
const debug = process.env.NODE_ENV !== 'production'
export default new Vuex.Store({
actions,
getters,
state,
mutations,
strict: false,
plugins: debug? [createLogger()] : []
})