一. 使用vuex修改state時,有兩種方式:
1)可以直接使用 this.$store.state.變量 = xxx;2)this.$store.dispatch(actionType, payload)
或者 this.$store.commit(commitType, payload)
二. 異同點
1)共同點: 能夠修改state裏的變量,並且是響應式的(能觸發視圖更新)2)不同點:
若將vue創建 store 的時候傳入 strict: true, 開啓嚴格模式,那麼任何修改state的操作,只要不經過
mutation的函數,
vue就會 throw error : [vuex] Do not mutate vuex store state outside mutation handlers。
(該報錯原因,請看源碼:https://blog.csdn.net/zhq2005095/article/details/78359883 );三.使用commit提交到mutation修改state的優點:
vuex能夠記錄每一次state的變化記錄,保存狀態快照,實現時間漫遊/回滾之類的操作。(實際本人未用到,暫時未遇到使用該特性的需求)