下面的代码证明不通过mutation,而直接修改state修改确实生效了。这样子多人协作岂不是很容易出问题。对于这个问题,在创建 store 的时候传入 strict: true, 开启严格模式,那么任何修改state的操作,只要不经过mutation的函数,vue就会 throw error。
const store = new Vuex.Store({ state: { count: 0 }, mutations: { increment (state) { state.count++ } } }) console.log(store.state.count) //0 store.state.count = 3; console.log(store.state.count) //3
参考文章: https://blog.csdn.net/weixin_40402192/article/details/80052887