Vuex 是一個專為 Vue.js 應用程序開發的狀態管理模式。它采用集中式存儲管理應用的所有組件的狀態,並以相應的規則保證狀態以一種可預測的方式發生變化。
狀態管理模式、集中式存儲管理,一聽就很高大上,蠻嚇人的。在我看來 vuex 就是把需要共享的變量全部存儲在一個對象里面,然后將這個對象放在頂層組件中供其他組件使用。這么說吧,將vue想作是一個js文件、組件是函數,那么vuex就是一個全局變量,只是這個“全局變量”包含了一些特定的規則而已。
在vue的組件化開發中,經常會遇到需要將當前組件的狀態傳遞給其他組件。父子組件通信時,我們通常會采用 props + emit 這種方式。但當通信雙方不是父子組件甚至壓根不存在相關聯系,或者一個狀態需要共享給多個組件時,就會非常麻煩,數據也會相當難維護,這對我們開發來講就很不友好。vuex 這個時候就很實用,不過在使用vuex之后也帶來了更多的概念和框架,需慎重!
一、問題引入
比如一個頁面有很多表單 , 我試圖將表單寫成一個單文件組件 , 但是表單 ( 子組件 ) 里的數據和頁面 ( 父組件 ) 按鈕交互的時候 , 它們之間的通訊很麻煩 :
<!--父組件中引入子組件-->
<template>
<div>
<a href="javascript:;" @click="show = true">點擊</a>
<t-dialog :show.sync="show"></t-dialog>
</div>
</template>
<script> import dialog from './components/dialog.vue' export default { data(){ return { show:false } }, components:{ "t-dialog":dialog } } </script>
<!--子組件-->
<template>
<el-dialog :visible.sync="currentShow"></el-dialog>
</template>
<script> export default { props:['show'], computed:{ currentShow:{ get(){ return this.show }, set(val){ this.$emit("update:show",val) } } } } </script>
之所以這么麻煩,是因為父組件可以通過 props
給子組件傳遞參數,但子組件內卻不能直接修改父組件傳過來的參數。
這時候,使用 vuex
就可以比較方便的解決這種問題了 :
//父組件中引入子組件
<template>
<div>
<a href="javascript:;" @click="$store.state.show = true">點擊</a>
<t-dialog></t-dialog>
</div>
</template>
<script> import dialog from './components/dialog.vue' export default { components:{ "t-dialog":dialog } } </script>
//子組件
<template>
<el-dialog :visible.sync="$store.state.show"></el-dialog>
</template>
<script> export default {} </script>
是不是方便了許多 , 這就是 vuex 最簡單的應用 , 不要被網上其他教程嚇到 , vuex 原來可以這么簡單 !
二、安裝、使用 vuex
首先我們在 vue.js 2.0 開發環境中安裝 vuex :npm install vuex --save
然后,在 main.js
中加入 :
import vuex from 'vuex' Vue.use(vuex); var store = new vuex.Store({//store對象
state:{ show:false } })
再然后 ,在實例化 Vue對象時加入 store 對象:
new Vue({ el: '#app', router, store,//使用store
template: '<App/>', components: { App } })
完成到這一步,上述例子中的 $store.state.show
就可以使用了。
三、modules
前面為了方便 , 我們把 store 對象寫在了 main.js 里面 , 但實際上為了便於日后的維護 , 我們分開寫更好 , 我們在 src
目錄下 , 新建一個 store
文件夾 , 然后在里面新建一個 index.js
:
import Vue from 'vue' import vuex from 'vuex' Vue.use(vuex); export default new vuex.Store({ state:{ show:false } })
那么相應的 , 在 main.js 里的代碼應該改成
//vuex
import store from './store'
new Vue({ el: '#app', router, store,//使用store
template: '<App/>', components: { App } })
這樣就把 store 分離出去了,那么還有一個問題是 : 這里 $store.state.show
無論哪個組件都可以使用 , 那組件多了之后 , 狀態也多了 , 這么多狀態都堆在 store 文件夾下的 index.js
不好維護怎么辦 ?
我們可以使用 vuex 的 modules
, 把 store 文件夾下的 index.js
改成 :
import Vue from 'vue' import vuex from 'vuex' Vue.use(vuex); import dialog_store from '../components/dialog_store.js';//引入某個store對象
export default new vuex.Store({ modules: { dialog: dialog_store } })
這里我們引用了一個 dialog_store.js
, 在這個 js 文件里我們就可以單獨寫 dialog 組件的狀態了:
export default { state:{ show:false } }
做出這樣的修改之后 , 我們將之前我們使用的 $store.state.show
統統改為 $store.state.dialog.show
即可。
如果還有其他的組件需要使用 vuex , 就新建一個對應的狀態文件 , 然后將他們加入 store 文件夾下的 index.js 文件中的 modules
中
modules: { dialog: dialog_store, other: other//其他組件
}
四、mutations
前面我們提到的對話框例子 , 我們對vuex 的依賴僅僅只有一個 $store.state.dialog.show
一個狀態 , 但是如果我們要進行一個操作 , 需要依賴很多很多個狀態 , 那管理起來又麻煩了 !
mutations
登場 , 問題迎刃而解 :
export default { state:{//state
show:false }, mutations:{ switch_dialog(state){//這里的state對應着上面這個state
state.show = state.show?false:true; //你還可以在這里執行其他的操作改變state
} } }
使用 mutations 后 , 原先我們的父組件可以改為 :
<template>
<div id="app">
<a href="javascript:;" @click="$store.commit('switch_dialog')">點擊</a>
<t-dialog></t-dialog>
</div>
</template>
<script> import dialog from './components/dialog.vue' export default { components:{ "t-dialog":dialog } } </script>
使用 $store.commit('switch_dialog')
來觸發 mutations
中的 switch_dialog
方法。
這里需要注意的是:
1、mutations
中的方法是不分組件的,假如你在 dialog_stroe.js 文件中的定義了switch_dialog
方法,在其他文件中的一個 switch_dialog
方法,那么 $store.commit('switch_dialog')
會執行所有的 switch_dialog
方法。
2、mutations
里的操作必須是同步的。
你一定好奇,如果在 mutations
里執行異步操作會發生什么事情,實際上並不會發生什么奇怪的事情,只是官方推薦,不要在 mutationss
里執行異步操作而已。
五、actions
多個 state
的操作 , 使用 mutations
會來觸發會比較好維護 , 那么需要執行多個 mutations 就需要用 action
了:
export default { state:{//state
show:false }, mutations:{ switch_dialog(state){//這里的state對應着上面這個state
state.show = state.show?false:true; //你還可以在這里執行其他的操作改變state
} }, actions:{ switch_dialog(context){//這里的context和我們使用的$store擁有相同的對象和方法 context.commit('switch_dialog'); //你還可以在這里觸發其他的mutations方法
}, } }
那么 , 在之前的父組件中 , 我們需要做修改 , 來觸發 action 里的 switch_dialog 方法:
<template>
<div id="app">
<a href="javascript:;" @click="$store.dispatch('switch_dialog')">點擊</a>
<t-dialog></t-dialog>
</div>
</template>
<script> import dialog from './components/dialog.vue' export default { components:{ "t-dialog":dialog } } </script>
使用 $store.dispatch('switch_dialog')
來觸發 action
中的 switch_dialog
方法。
官方推薦 , 將異步操作放在 action 中。
六、getters
getters
和 vue 中的 computed
類似 , 都是用來計算 state, 然后生成新的數據 ( 狀態 ) 的。
還是前面的例子 , 假如我們需要一個與狀態 show
剛好相反的狀態 , 使用 vue 中的 computed
可以這樣算出來 :
computed(){ not_show(){ return !this.$store.state.dialog.show; } }
那么 , 如果很多很多個組件中都需要用到這個與 show 剛好相反的狀態 , 那么我們需要寫很多很多個 not_show
, 使用 getters
就可以解決這種問題 :
export default { state:{//state
show:false }, getters:{ not_show(state){//這里的state對應着上面這個state
return !state.show; } }, mutations:{ switch_dialog(state){//這里的state對應着上面這個state
state.show = state.show?false:true; //你還可以在這里執行其他的操作改變state
} }, actions:{ switch_dialog(context){//這里的context和我們使用的$store擁有相同的對象和方法
context.commit('switch_dialog'); //你還可以在這里觸發其他的mutations方法
}, } }
我們在組件中使用 $store.state.dialog.show
來獲得狀態 show
, 類似的 , 我們可以使用 $store.getters.not_show
來獲得狀態 not_show
。
注意:$store.getters.not_show
的值是不能直接修改的 , 需要對應的 state 發生變化才能修改。
七、mapState、mapGetters、mapActions
很多時候,$store.state.dialog.show
、$store.dispatch('switch_dialog')
這種寫法又長又臭 , 很不方便 , 我們沒使用 vuex 的時候 , 獲取一個狀態只需要 this.show
, 執行一個方法只需要 this.switch_dialog
就行了 , 使用 vuex 使寫法變復雜了 ?
使用 mapState、mapGetters、mapActions
就不會這么復雜了。
以 mapState 為例 :
<template>
<el-dialog :visible.sync="show"></el-dialog>
</template>
<script> import {mapState} from 'vuex'; export default { computed:{ //這里的三點叫做 : 擴展運算符
...mapState({ show:state=>state.dialog.show }), } } </script>
//相當於 :
<template>
<el-dialog :visible.sync="show"></el-dialog>
</template>
<script> import {mapState} from 'vuex'; export default { computed:{ show(){ return this.$store.state.dialog.show; } } } </script>
mapGetters、mapActions 和 mapState 類似 , mapGetters
一般也寫在 computed
中 , mapActions
一般寫在 methods
中。
弄懂上面這些 , 可以去看vuex文檔了 , 應該能看清晰很多了。