1. 程式人生 > >vue之vuex入門祕籍

vue之vuex入門祕籍

如果你在使用 vue.js , 那麼我想你可能會對 vue 元件之間的通訊感到崩潰 。

我在使用基於 vue.js 2.0 的UI框架 ElementUI 開發網站的時候 , 就遇到了這種問題 : 一個頁面有很多表單 , 我試圖將表單寫成一個單檔案元件 , 但是表單 ( 子元件 ) 裡的資料和頁面 ( 父元件 ) 按鈕互動的時候 , 它們之間的通訊很麻煩 :

  1. <!--父元件中引入子元件-->

  2. <template>

  3. <div>

  4. <a href="javascript:;" @click="show = true">點選</a>

  5. <t-dialog :show.sync="show"></t-dialog>

  6. </div>

  7. </template>

  8. <script>

  9. import dialog from './components/dialog.vue'

  10. export default {

  11. data(){

  12. return {

  13. show:false

  14. }

  15. },

  16. components:{

  17. "t-dialog":dialog

  18. }

  19. }

  20. </script>

  21. <!--子元件-->

  22. <template>

  23. <el-dialog :visible.sync="currentShow"></el-dialog>

  24. </template>

  25. <script>

  26. export default {

  27. props:['show'],

  28. computed:{

  29. currentShow:{

  30. get(){

  31. return this.show

  32. },

  33. set(val){

  34. this.$emit("update:show",val)

  35. }

  36. }

  37. }

  38. }

  39. </script>

之所以這麼麻煩 , 是因為父元件可以通過 props 給子元件傳遞引數 , 但子元件內卻不能直接修改父元件傳過來的引數。

這時候 , 使用 vuex 就可以比較方便的解決這種問題了 :

  1. <!--父元件中引入子元件-->

  2. <template>

  3. <div>

  4. <a href="javascript:;" @click="$store.state.show = true">點選</a>

  5. <t-dialog></t-dialog>

  6. </div>

  7. </template>

  8. <script>

  9. import dialog from './components/dialog.vue'

  10. export default {

  11. components:{

  12. "t-dialog":dialog

  13. }

  14. }

  15. </script>

  16. <!--子元件-->

  17. <template>

  18. <el-dialog :visible.sync="$store.state.show"></el-dialog>

  19. </template>

  20. <script>

  21. export default {}

  22. </script>

是不是方便了許多 , 這就是 vuex 最簡單的應用 , 不要被網上其他教程嚇到 , vuex 原來可以這麼簡單 !

安裝、使用 vuex

首先我們在 vue.js 2.0 開發環境中安裝 vuex :

  1. npm install vuex --save

然後 , 在 main.js 中加入 :

  1. import vuex from 'vuex'

  2. Vue.use(vuex);

  3. var store = new vuex.Store({//store物件

  4. state:{

  5. show:false

  6. }

  7. })

再然後 , 在例項化 Vue物件時加入 store 物件 :

  1. new Vue({

  2. el: '#app',

  3. router,

  4. store,//使用store

  5. template: '<App/>',

  6. components: { App }

  7. })

完成到這一步 , 上述例子中的 $store.state.show 就可以使用了。

modules

前面為了方便 , 我們把 store 物件寫在了 main.js 裡面 , 但實際上為了便於日後的維護 , 我們分開寫更好 , 我們在 src 目錄下 , 新建一個 store 資料夾 , 然後在裡面新建一個 index.js :

  1. import Vue from 'vue'

  2. import vuex from 'vuex'

  3. Vue.use(vuex);

  4. export default new vuex.Store({

  5. state:{

  6. show:false

  7. }

  8. })

那麼相應的 , 在 main.js 裡的程式碼應該改成 :

  1. //vuex

  2. import store from './store'

  3. new Vue({

  4. el: '#app',

  5. router,

  6. store,//使用store

  7. template: '<App/>',

  8. components: { App }

  9. })

這樣就把 store 分離出去了 , 那麼還有一個問題是 : 這裡 $store.state.show 無論哪個元件都可以使用 , 那元件多了之後 , 狀態也多了 , 這麼多狀態都堆在 store 資料夾下的 index.js 不好維護怎麼辦 ?

我們可以使用 vuex 的 modules , 把 store 資料夾下的 index.js 改成 :

  1. import Vue from 'vue'

  2. import vuex from 'vuex'

  3. Vue.use(vuex);

  4. import dialog_store from '../components/dialog_store.js';//引入某個store物件

  5. export default new vuex.Store({

  6. modules: {

  7. dialog: dialog_store

  8. }

  9. })

這裡我們引用了一個 dialog_store.js , 在這個 js 檔案裡我們就可以單獨寫 dialog 元件的狀態了 :

  1. export default {

  2. state:{

  3. show:false

  4. }

  5. }

做出這樣的修改之後 , 我們將之前我們使用的 $store.state.show 統統改為 $store.state.dialog.show 即可。

如果還有其他的元件需要使用 vuex , 就新建一個對應的狀態檔案 , 然後將他們加入 store 資料夾下的 index.js 檔案中的 modules 中。

  1. modules: {

  2. dialog: dialog_store,

  3. other: other,//其他元件

  4. }

mutations

前面我們提到的對話方塊例子 , 我們對vuex 的依賴僅僅只有一個 $store.state.dialog.show 一個狀態 , 但是如果我們要進行一個操作 , 需要依賴很多很多個狀態 , 那管理起來又麻煩了 !

mutations 登場 , 問題迎刃而解 :

  1. export default {

  2. state:{//state

  3. show:false

  4. },

  5. mutations:{

  6. switch_dialog(state){//這裡的state對應著上面這個state

  7. state.show = state.show?false:true;

  8. //你還可以在這裡執行其他的操作改變state

  9. }

  10. }

  11. }

使用 mutations 後 , 原先我們的父元件可以改為 :

  1. <template>

  2. <div id="app">

  3. <a href="javascript:;" @click="$store.commit('switch_dialog')">點選</a>

  4. <t-dialog></t-dialog>

  5. </div>

  6. </template>

  7. <script>

  8. import dialog from './components/dialog.vue'

  9. export default {

  10. components:{

  11. "t-dialog":dialog

  12. }

  13. }

  14. </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 了:

  1. export default {

  2. state:{//state

  3. show:false

  4. },

  5. mutations:{

  6. switch_dialog(state){//這裡的state對應著上面這個state

  7. state.show = state.show?false:true;

  8. //你還可以在這裡執行其他的操作改變state

  9. }

  10. },

  11. actions:{

  12. switch_dialog(context){//這裡的context和我們使用的$store擁有相同的物件和方法

  13. context.commit('switch_dialog');

  14. //你還可以在這裡觸發其他的mutations方法

  15. },

  16. }

  17. }

那麼 , 在之前的父元件中 , 我們需要做修改 , 來觸發 action 裡的 switch_dialog 方法:

  1. <template>

  2. <div id="app">

  3. <a href="javascript:;" @click="$store.dispatch('switch_dialog')">點選</a>

  4. <t-dialog></t-dialog>

  5. </div>

  6. </template>

  7. <script>

  8. import dialog from './components/dialog.vue'

  9. export default {

  10. components:{

  11. "t-dialog":dialog

  12. }

  13. }

  14. </script>

使用 $store.dispatch('switch_dialog') 來觸發 action 中的 switch_dialog 方法。

官方推薦 , 將非同步操作放在 action 中。

getters

getters 和 vue 中的 computed 類似 , 都是用來計算 state 然後生成新的資料 ( 狀態 ) 的。

還是前面的例子 , 假如我們需要一個與狀態 show 剛好相反的狀態 , 使用 vue 中的 computed 可以這樣算出來 :

  1. computed(){

  2. not_show(){

  3. return !this.$store.state.dialog.show;

  4. }

  5. }

那麼 , 如果很多很多個元件中都需要用到這個與 show 剛好相反的狀態 , 那麼我們需要寫很多很多個 not_show , 使用 getters 就可以解決這種問題 :

  1. export default {

  2. state:{//state

  3. show:false

  4. },

  5. getters:{

  6. not_show(state){//這裡的state對應著上面這個state

  7. return !state.show;

  8. }

  9. },

  10. mutations:{

  11. switch_dialog(state){//這裡的state對應著上面這個state

  12. state.show = state.show?false:true;

  13. //你還可以在這裡執行其他的操作改變state

  14. }

  15. },

  16. actions:{

  17. switch_dialog(context){//這裡的context和我們使用的$store擁有相同的物件和方法

  18. context.commit('switch_dialog');

  19. //你還可以在這裡觸發其他的mutations方法

  20. },

  21. }

  22. }

我們在元件中使用 $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 為例 :

  1. <template>

  2. <el-dialog :visible.sync="show"></el-dialog>

  3. </template>

  4. <script>

  5. import {mapState} from 'vuex';

  6. export default {

  7. computed:{

  8. //這裡的三點叫做 : 擴充套件運算子

  9. ...mapState({

  10. show:state=>state.dialog.show

  11. }),

  12. }

  13. }

  14. </script>

相當於 :

  1. <template>

  2. <el-dialog :visible.sync="show"></el-dialog>

  3. </template>

  4. <script>

  5. import {mapState} from 'vuex';

  6. export default {

  7. computed:{

  8. show(){

  9. return this.$store.state.dialog.show;

  10. }

  11. }

  12. }

  13. </script>

mapGetters、mapActions 和 mapState 類似 , mapGetters 一般也寫在 computed 中 , mapActions 一般寫在 methods 中。

弄懂上面這些 , 你可以去看vuex文件了 , 應該能看懂了

轉自:https://segmentfault.com/a/1190000009404727