Vuex核心知識(2.0)


Vuex 是一個專門為 Vue.js 應該程序開發的狀態管理模式,它類似於 Redux 應用於 React 項目中,他們都是一種 Flux 架構。相比 Redux,Vuex 更簡潔,學習成本更低。希望通過本文幫助還沒使用 Vuex 的同學快速上手。

注:本文針對 Vuex 2.0 的語法,目前通過 npm 默認下載的版本為 1.0+ ,想引入 2.0 版本可以通過 script 標簽引入。

<script src="https://unpkg.com/vuex@2.0.0"></script>

習慣使用 ES6 中 import 方法的可以暫時通過解構賦值的方式引入 Vuex 中的方法。

import { mapState, mapGetters } from 'Vuex';
//替換為:
let { mapState, mapGetters } = Vuex;

 

Vuex 的核心內容主要就是 StateGettersMutationsActions 這四部分,也非常好理解。

State

首先看如何申明一個 store

import Vue from 'Vue';
import Vuex from 'Vuex';

Vue.use(Vuex);

let store = new Vuex.Store({
    state: {
        stateA: 'a',
        stateB: 'b',
        stateC: 'c'
    }
});

console.log(store.state.stateA); // a

在 store 中的 state 對象,可以理解為 Vue 實例中的 data 對象,它用來保存最基本的數據。

在 Vue 中獲取 store 中的狀態

let app = new Vue({
   el: '#demo', template: `
<h1>{{myState}}</h1>`, computed: { myState() { return store.state.stateA; } } });

最簡單的方式就是通過 Vue 中的計算屬性(computed) 來將 store 中的狀態映射為 Vue 的數據。但是當數據多時這種方法明顯效率過低,所以 Vuex 中提供了 mapState 方法用於批量映射 store 中的狀態。

首先必須在 Vue 中注冊 store 選項,這樣整個 store 就從根節點注冊到 Vue 下的每一個子組件里了。 

import { mapState } from 'Vuex';

let app = new Vue({
el: '#demo',
store, data: { local:
'L' }, computed: mapState({ stateA: state => state.stateA, stateB: 'stateB', stateC(state) { return state.stateC + this.local; } }) });

上例中,a. 可以通過 ES6 中的箭頭函數進行數據的映射,b. 當計算屬性的名稱與 state 的屬性名一致時可能直接通過字符串賦值,c. 當需要引用上下文中的 data 屬性實,只能通過常規函數來使 this 生效。

如果所有計算屬性的名稱都與 state 一致,可以在 mapState 中以數組的方式進行映射。如果 Vue 中已經存在計算屬性,可以通過 ES6 的展開操作符 (...) 進行組合。

let app = new Vue({
    el: '#demo',
store, computed: { local() {
return 'Local'; }, ...mapState(['stateA', 'stateB', 'stateC']) } });

 

Getters

當需要對 store 中的數據進行處理,或者需要對處理后的數據進行復用,就可以使用 Getters 來處理,Getters 也可以理解為 Vue 中的計算屬性 (computed)。

let store = new Vuex.Store({
    state: {
        nowDate: new Date()
    },
    getters: {
        dateFormat(state, getters) {
            let date = state.nowDate;
            return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()} / ${date.getHours()}:${date.getMinutes()}`;
        }
    }
});

console.log('The time is now:', store.getters.dateFormat); // The time is now: 2017-2-10 / 17:28

getters 接收兩個參數,1. state、2. getters 本身,可以引用其他 getters。與 mapState 類似,Getters 也擁有 mapGetters 方法,用於批量映射。

let { mapGetters } from 'Vuex';

let comonent = {
    computed: {
        ...mapGetters([
            'nowDate'
        ])
    }
};

 

Mutations

在 Vue 中,data 值是可以直接被更改的。但是在 Vuex 中,不能直接對 state 進行操作,唯一的方法就是提交 mutation。mutation 可以理解為 Vue 中的 method 事件,只不過調用 mutation 需要特別的方法。

let store = new Vuex.Store({
    state: {
        count: 0
    },
    mutations: {
        addCount(state) {
            state.count ++;
        }
    }
});

store.commit('addCount');

console.log(store.state.count); // 1

每一個 mutation 都有一個字符串的事件類型和一個回調函數。通常在回調函數中進行狀態更改,通過 store.commit 觸發事件。 

傳參

// ...
mutations: {
    addCount(state, n) {
        state.count += n;
    }
}

store.commit('addCount', 10);

 這種方式有一個問題,一旦參數多了,就必須保證傳入的順序不能出錯,可讀性也很低。所以通常將參數以對象的形式傳入,同時 mutaion 的事件類型字符串也可以使用對象的形式傳入。

// ...
mutations: {
    addCount(state, params) {
        state.count += params.num;
    }
}

store.commit('addCount', {
    num: 10
});

store.commit({
    type: 'addCount',
    num: 10
});

這里有一個規則需要遵守,在 mutation 中更改 state 應該以新對象替換老對象,不要在直接原對象上直接修改。*熟悉 React 的朋友們應該知道,在使用 setState 更新狀態時也是同樣的規則。

通過 ES6 的展開操作符可以很容易的完成。

state.obj = { ...state.obj, newState: 123 };

在 Vue 組件中提交 mutaion

this.$store.commit('xxx');

在組件中可以通過上述方法提交 commit,不過 Vuex 提供了 mapMutations 方法用於將 mutation 映射到組件中的 method 中。與 mapState、mapGetters 相似,這里就不再贅述了。

import { mapMutations } from 'vuex'

const component = {
    // ...
    methods: {
        ...mapMutations([
            'addCount' // 映射 this.addCount() 為 this.$store.commit('addCount')
        ]),
        ...mapMutations({
            add: 'addCount' // 映射 this.add() 為 this.$store.commit('addCount')
        })
    }
} 

mutation 必須是同步函數

我們先試着寫一個異步的 mutation ,看看會發生什么。

// ...
mutations: {
    asyncAdd(state) {
        setTimeout(() => {
            state.count ++;
        }, 2000);
    }
}

store.commit('asyncAdd');

經測試,在 mutaion 里進行異步操作也是會生效的,2秒之后 state.count 確實發生改變。

那為什么還要強調 mutation 必須是同步的呢?因為在觸發 commit 之后,回調函數還沒有被調用,所以這次 mutation 的修改也是無法被調試工具所記錄的。

如何對 state 進行異步操作呢,就要使用下面的 Action 了。

 

Actions

Action 類似於 mutation,不同在於:

1. Action 不直接更改狀態,而是提交 mutation

2. Action 可以包含任何異步操作

const store = new Vuex.Store({
    state: {
        count: 0
    },
    mutations: {
        addCount(state) {
            state.count ++;
        }
    },
    actions: {
       asyncAdd(context) {
           setTimeout(() => {
               context.commit('addCount');
           }, 2000);
       }
    } 
}) 

Action 中的回調函數會接收一個上下文 context 對象,它包含了當前 store 中所有屬性和方法,但其不是 store 本身。你可以通過 context.commit 來提交 mutation,也可以通過 context.state 與 context.getters 來獲取 state 和 getters。

當需要多次調用 commit 時,可以使用 ES6 的語法對傳入的參數進行解構。

// ...
actions: {
    asyncAdd({ commit }) {
        commit('addCount');
    }
} 

分發 Action 與傳參

Action 是通過 store.dispatch 方法來觸發,傳參方式與 store.commit 類似。

store.dispatch('asyncAdd');

store.dispatch('asyncAdd', {
    num: 10
});

store.dispatch({
    type: 'asyncAdd',
    num: 10
});

在 Vue 組件中分發 Action

this.$store.dispatch('xxx');

可以使用上述方式,同時 Vuex 中也提供了 mapActions 方法用於批量映射於組件中的 methods 中,與 mapMutations 類似。

import { mapActions } from 'vuex'

const component = {
    // ...
    methods: {
        ...mapActions([
            'asyncAdd' // 映射 this.asyncAdd() 為 this.$store.dispatch('asyncAdd')
        ]),
        ...mapActions({
            add: 'asyncAdd' // 映射 this.add() 為 this.$store.dispatch('asyncAdd')
        })
    }
}

組合 Actions

既然 Action 是異步行為,那我們可以使用 ES6 中的 Promise 對象進行組合。

const store = {
    actions: {
        asyncActionA({ commit }) {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    commit('asyncAdd');
                    resolve();
                }, 2000);
            });
        },
        asyncActionB({ dispatch }, params) {
            return dispatch('asyncActionA').then(() => {
                console.log('Action complete at: ', params.date);
            });
        }
    }
}

store.dispatch('asyncActionB', {
    date: (new Date()).getTime() // 2秒后打印 Action complete at: xxxxxxxx (當前時間毫秒數)
});

 

本文介紹了 Vuex 的核心知識,在下一篇博文中我介紹 Vuex 如何進行模塊化以及工程目錄結構創建。

即使項目不大,但當數據交互較多,尤其是跨組件的事件觸發較多時,單純的使用 Vue 進行項目開發就顯得有些吃力。使用 Vuex 可以幫我們很好的管理數據模型,希望已經迷上使用 Vue 進行開發的同學能很好的利用起來。

 

感謝你的瀏覽,希望能有所幫助。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM