vuex 是一個專爲 Vue.js 應用程序開發的狀態管理模式。它採用集中式存儲管理應用的全部組件的狀態,並以相應的規則保證狀態以一種可預測的方式發生變化。
首先咱們在 vue.js 2.0 開發環境中安裝 vuex :vue
npm install vuex --save
而後 , 在 main.js 中加入 :vuex
import vuex from 'vuex' Vue.use(vuex); const store = new vuex.Store({//store對象 state:{ show:false, count:0 } })
再而後 , 在實例化 Vue對象時加入 store 對象 :npm
new Vue({ el: '#app', router, store,//使用store template: '<App/>', components: { App } })
如今,你能夠經過 store.state 來獲取狀態對象,以及經過 store.commit 方法觸發狀態變動:數組
store.commit('increment') console.log(store.state.count) // -> 1
從 store 實例中讀取狀態最簡單的方法就是在計算屬性中返回某個狀態:緩存
// 建立一個 Counter 組件 const Counter = { template: `<div>{{ count }}</div>`, computed: { count () { return this.$store.state.count } } }
當一個組件須要獲取多個狀態時候,將這些狀態都聲明爲計算屬性會有些重複和冗餘。爲了解決這個問題,咱們可使用 mapState 輔助函數幫助咱們生成計算屬性:app
// 在單獨構建的版本中輔助函數爲 Vuex.mapState import { mapState } from 'vuex' export default { // ... computed: mapState({ // 箭頭函數可以使代碼更簡練 count: state => state.count, // 傳字符串參數 'count' 等同於 `state => state.count` countAlias: 'count', // 爲了可以使用 `this` 獲取局部狀態,必須使用常規函數 countPlusLocalState (state) { return state.count + this.localCount } }) }
當映射的計算屬性的名稱與 state 的子節點名稱相同時,咱們也能夠給 mapState 傳一個字符串數組:異步
computed: mapState([ // 映射 this.count 爲 store.state.count 'count' ])
getters 和 vue 中的 computed 相似 , 都是用來計算 state 而後生成新的數據 ( 狀態 ) 的,就像計算屬性同樣,getter 的返回值會根據它的依賴被緩存起來,且只有當它的依賴值發生了改變纔會被從新計算。函數
Getter 接受 state 做爲其第一個參數:this
const store = new Vuex.Store({ state: { todos: [ { id: 1, text: '...', done: true }, { id: 2, text: '...', done: false } ] }, getters: { doneTodos: state => { return state.todos.filter(todo => todo.done) } } })
Getter 會暴露爲 store.getters 對象,你能夠以屬性的形式訪問這些值:spa
store.getters.doneTodos // -> [{ id: 1, text: '...', done: true }]
Getter 也能夠接受其餘 getter 做爲第二個參數:
getters: { // ... doneTodosCount: (state, getters) => { return getters.doneTodos.length } } store.getters.doneTodosCount // -> 1
組件中使用:
computed: { doneTodosCount () { return this.$store.getters.doneTodosCount } }
注意,getter 在經過屬性訪問時是做爲 Vue 的響應式系統的一部分緩存其中的。
你也能夠經過讓 getter 返回一個函數,來實現給 getter 傳參。在你對 store 裏的數組進行查詢時很是有用:
getters: { // ... getTodoById: (state) => (id) => { return state.todos.find(todo => todo.id === id) } }
store.getters.getTodoById(2) // -> { id: 2, text: '...', done: false }
注意,getter 在經過方法訪問時,每次都會去進行調用,而不會緩存結果。
mapGetters 輔助函數僅僅是將 store 中的 getter 映射到局部計算屬性:
import { mapGetters } from 'vuex' export default { // ... computed: { // 使用對象展開運算符將 getter 混入 computed 對象中 ...mapGetters([ 'doneTodosCount', 'anotherGetter', // ... ]) } }
若是你想將一個 getter 屬性另取一個名字,使用對象形式:
mapGetters({ // 把 `this.doneCount` 映射爲 `this.$store.getters.doneTodosCount` doneCount: 'doneTodosCount' })
更改 Vuex 的 store 中的狀態的惟一方法是提交 mutation。
註冊:
const store = new Vuex.Store({ state: { count: 1 }, mutations: { increment (state) { // 變動狀態 state.count++ } } })
調用:
store.commit('increment')
你能夠向 store.commit 傳入額外的參數,即 mutation 的 載荷(payload):
// ... mutations: { increment (state, n) { state.count += n } }
store.commit('increment', 10)
若是提交多個參數,必須使用對象的形式進行提交
// ... mutations: { increment (state, payload) { state.count += payload.amount } }
store.commit('increment', { amount: 10 })
注:mutations裏的操做必須是同步的;
Action 相似於 mutation,不一樣在於:
const store = new Vuex.Store({ state: { count: 0 }, mutations: { increment (state) { state.count++ } }, actions: { increment (context) { context.commit('increment') } } })
Action 經過 store.dispatch 方法觸發:
store.dispatch('increment')
在 action 內部執行異步操做:
actions: { incrementAsync ({ commit }) { setTimeout(() => { commit('increment') }, 1000) } }
對象形式傳參:
// 以載荷形式分發 store.dispatch('incrementAsync', { amount: 10 })