Vuex之理解Mutations的用法實例
1.什么是mutations?
上一篇文章說的getters
是為了初步獲取和簡單處理state
里面的數(shù)據(jù)(這里的簡單處理不能改變state里面的數(shù)據(jù)),Vue
的視圖是由數(shù)據(jù)驅(qū)動的,也就是說state
里面的數(shù)據(jù)是動態(tài)變化的,那么怎么改變呢,切記在Vuex
中store
數(shù)據(jù)改變的唯一方法就是mutation
!
通俗的理解mutations
,里面裝著一些改變數(shù)據(jù)方法的集合,這是Veux
設(shè)計很重要的一點,就是把處理數(shù)據(jù)邏輯方法全部放在mutations
里面,使得數(shù)據(jù)和視圖分離。
2.怎么用mutations?
mutation結(jié)構(gòu):每一個mutation
都有一個字符串類型的事件類型(type
)和回調(diào)函數(shù)(handler
),也可以理解為{type:handler()},
這和訂閱發(fā)布有點類似。先注冊事件,當(dāng)觸發(fā)響應(yīng)類型的時候調(diào)用handker()
,調(diào)用type
的時候需要用到store.commit
方法。
const store = new Vuex.Store({ state: { count: 1 }, mutations: { increment (state) { //注冊時間,type:increment,handler第一個參數(shù)是state; // 變更狀態(tài) state.count++}}}) store.commit('increment') //調(diào)用type,觸發(fā)handler(state)
載荷(payload):簡單的理解就是往handler(stage)
中傳參handler(stage,pryload)
;一般是個對象。
mutations: { increment (state, n) { state.count += n}} store.commit('increment', 10) mutation-types:將常量放在單獨的文件中,方便協(xié)作開發(fā)。 // mutation-types.js export const SOME_MUTATION = 'SOME_MUTATION' // store.js import Vuex from 'vuex' import { SOME_MUTATION } from './mutation-types' const store = new Vuex.Store({ state: { ... }, mutations: { // 我們可以使用 ES2015 風(fēng)格的計算屬性命名功能來使用一個常量作為函數(shù)名 [SOME_MUTATION] (state) { // mutate state } } })
commit:提交可以在組件中使用 this.$store.commit('xxx')
提交 mutation
,或者使用 mapMutations
輔助函數(shù)將組件中的 methods
映射為 store.commit
調(diào)用(需要在根節(jié)點注入 store
)。
import { mapMutations } from 'vuex' export default { methods: { ...mapMutations([ 'increment' // 映射 this.increment() 為 this.$store.commit('increment')]), ...mapMutations({ add: 'increment' // 映射 this.add() 為 this.$store.commit('increment') })}}
3.源碼分析
registerMutation
:初始化mutation
function registerMutation (store, type, handler, path = []) { //4個參數(shù),store是Store實例,type為mutation的type,handler,path為當(dāng)前模塊路徑 const entry = store._mutations[type] || (store._mutations[type] = []) //通過type拿到對應(yīng)的mutation對象數(shù)組 entry.push(function wrappedMutationHandler (payload) { //將mutation包裝成函數(shù)push到數(shù)組中,同時添加載荷payload參數(shù) handler(getNestedState(store.state, path), payload) //通過getNestedState()得到當(dāng)前的state,同時添加載荷payload參數(shù) }) }
commit
:調(diào)用mutation
commit (type, payload, options) { // 3個參數(shù),type是mutation類型,payload載荷,options配置 if (isObject(type) && type.type) { // 當(dāng)type為object類型, options = payload payload = type type = type.type } const mutation = { type, payload } const entry = this._mutations[type] // 通過type查找對應(yīng)的mutation if (!entry) { //找不到報錯 console.error(`[vuex] unknown mutation type: ${type}`) return } this._withCommit(() => { entry.forEach(function commitIterator (handler) { // 遍歷type對應(yīng)的mutation對象數(shù)組,執(zhí)行handle(payload)方法 //也就是開始執(zhí)行wrappedMutationHandler(handler) handler(payload) }) }) if (!options || !options.silent) { this._subscribers.forEach(sub => sub(mutation, this.state)) //把mutation和根state作為參數(shù)傳入 } }
subscribers
:訂閱store
的mutation
subscribe (fn) { const subs = this._subscribers if (subs.indexOf(fn) < 0) { subs.push(fn) } return () => { const i = subs.indexOf(fn) if (i > -1) { subs.splice(i, 1) } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vuex中store的action和mutations用法
- vuex?mutations的兩種調(diào)用方法小結(jié)
- Vuex中的Mutations的具體使用方法
- Vuex modules模式下mapState/mapMutations的操作實例
- 在Vuex使用dispatch和commit來調(diào)用mutations的區(qū)別詳解
- vuex中的 mapState,mapGetters,mapActions,mapMutations 的使用
- 詳解vuex中mapState,mapGetters,mapMutations,mapActions的作用
- Vuex中mutations與actions的區(qū)別詳解
- vuex mutations 同步操作方法詳解
相關(guān)文章
vuex數(shù)據(jù)持久化的兩種實現(xiàn)方案
在vuex的時候刷新以后里面存儲的state就會被瀏覽器釋放掉,因為我們的state都是存儲在內(nèi)存中的,所以一刷新頁面就會把state中的數(shù)據(jù)重置,這就涉及到vue數(shù)據(jù)持久化的問題,這篇文章主要給大家介紹了關(guān)于vuex數(shù)據(jù)持久化的兩種實現(xiàn)方案,需要的朋友可以參考下2021-07-07使用Vue CLI創(chuàng)建typescript項目的方法
這篇文章主要介紹了使用Vue CLI創(chuàng)建typescript項目的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Elementui如何限制el-input框輸入小數(shù)點
這篇文章主要介紹了Elementui如何限制el-input框輸入小數(shù)點,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08vue實現(xiàn)簡易圖片左右旋轉(zhuǎn),上一張,下一張組件案例
這篇文章主要介紹了vue實現(xiàn)簡易圖片左右旋轉(zhuǎn),上一張,下一張組件案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07Vue框架中如何調(diào)用模擬數(shù)據(jù)你知道嗎
這篇文章主要為大家詳細(xì)介紹了Vue框架中如何調(diào)用模擬數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03淺談vue-cli加載不到dev-server.js的解決辦法
本篇文章主要介紹了淺談vue-cli加載不到dev-server.js的解決辦法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11