Vuex中的Modules你知道多少?

开发 前端
为什么会出现VueX的模块呢?当你的项目中代码变多的时候,很难区分维护。那么这时候Vuex的模块功能就这么体现出来了。

 [[413343]]

为什么会出现VueX的模块呢?当你的项目中代码变多的时候,很难区分维护。那么这时候Vuex的模块功能就这么体现出来了。

那么我们就开始吧!

一、模块是啥?

  1. /* eslint-disable no-unused-vars */ 
  2. import Vue from 'vue' 
  3. import Vuex from 'vuex' 
  4.  
  5. Vue.use(Vuex) 
  6.  
  7. export default new Vuex.Store({ 
  8.   state:{ 
  9.     global:'this is global' 
  10.   }, 
  11.   // 在以下属性可以添加多个模块。如:moduleOne模块、moduleTwo模块。 
  12.   modules: { 
  13.     moduleOne:{}, 
  14.     moduleTwo:{} 
  15.   } 
  16. }) 

二、在模块内添加state

可以直接在模块中直接书写state对象。

  1. /* eslint-disable no-unused-vars */ 
  2. import Vue from 'vue' 
  3. import Vuex from 'vuex' 
  4.  
  5. Vue.use(Vuex) 
  6.  
  7. export default new Vuex.Store({ 
  8.   state:{ 
  9.     global:'this is global' 
  10.   }, 
  11.   modules: { 
  12.     moduleOne:{ 
  13.       state:{ 
  14.         moduleOnevalue:'1' 
  15.       } 
  16.       
  17.     }, 
  18.     moduleTwo:{ 
  19.       state:{ 
  20.         moduleTwovalue:'0' 
  21.       } 
  22.     } 
  23.   } 
  24. }) 

我们在页面中引用它。我们直接可以找到对应的模块返回值,也可以使用mapState方法调用。

  1. <template> 
  2.     <div class="home"
  3.         <p>moduleOne_state:{{moduleOne}}</p> 
  4.         <p>moduleTwo_state:{{moduleTwo}}</p> 
  5.         <p>moduleOne_mapState:{{moduleOnevalue}}</p> 
  6.         <p>moduleTwo_mapState:{{moduleTwovalue}}</p> 
  7.     </div> 
  8. </template> 
  9. <script> 
  10. import {mapState} from 'vuex' 
  11. export default { 
  12.     name:"Home"
  13.     data() { 
  14.         return { 
  15.             msg:"this is Home" 
  16.         } 
  17.     }, 
  18.     computed: { 
  19.         moduleOne(){ 
  20.             // 这里使用了命名空间 
  21.             return this.$store.state.moduleOne.moduleOnevalue 
  22.         }, 
  23.         moduleTwo(){ 
  24.             return this.$store.state.moduleTwo.moduleTwovalue 
  25.         }, 
  26.         ...mapState({ 
  27.            moduleOnevalue:(state)=>state.moduleOne.moduleOnevalue, 
  28.            moduleTwovalue:(state)=>state.moduleTwo.moduleTwovalue 
  29.         }) 
  30.     }, 
  31.     methods: { 
  32.   
  33.     }, 
  34.     mounted() { 
  35.  
  36.     }, 
  37. </script> 

三、在模块中添加mutations

我们分别给两个模块添加mutations属性,在里面定义相同名字的方法,我们先在页面看一下。

  1. /* eslint-disable no-unused-vars */ 
  2. import Vue from 'vue' 
  3. import Vuex from 'vuex' 
  4.  
  5. Vue.use(Vuex) 
  6.  
  7. export default new Vuex.Store({ 
  8.   state:{ 
  9.     global:'this is global' 
  10.   }, 
  11.   modules: { 
  12.     moduleOne:{ 
  13.       state:{ 
  14.         moduleOnevalue:'1' 
  15.       }, 
  16.       mutations:{ 
  17.         updateValue(state,value){ 
  18.           state.moduleOnevalue=value 
  19.         } 
  20.       } 
  21.       
  22.     }, 
  23.     moduleTwo:{ 
  24.       state:{ 
  25.         moduleTwovalue:'0' 
  26.       }, 
  27.       mutations:{ 
  28.         updateValue(state,value){ 
  29.           state.moduleTwovalue=value 
  30.         } 
  31.       }  
  32.     } 
  33.   } 
  34. }) 

在页面引用它

  1. <template> 
  2.     <div class="home"
  3.         <p>moduleOne_state:{{moduleOne}}</p> 
  4.         <p>moduleTwo_state:{{moduleTwo}}</p> 
  5.         <p>moduleOne_mapState:{{moduleOnevalue}}</p> 
  6.         <p>moduleTwo_mapState:{{moduleTwovalue}}</p> 
  7.     </div> 
  8. </template> 
  9. <script> 
  10. // eslint-disable-next-line no-unused-vars 
  11. import {mapState,mapMutations} from 'vuex' 
  12. export default { 
  13.     name:"Home"
  14.     data() { 
  15.         return { 
  16.             msg:"this is Home" 
  17.         } 
  18.     }, 
  19.     computed: { 
  20.         moduleOne(){ 
  21.             return this.$store.state.moduleOne.moduleOnevalue 
  22.         }, 
  23.         moduleTwo(){ 
  24.             return this.$store.state.moduleTwo.moduleTwovalue 
  25.         }, 
  26.         ...mapState({ 
  27.            moduleOnevalue:(state)=>state.moduleOne.moduleOnevalue, 
  28.            moduleTwovalue:(state)=>state.moduleTwo.moduleTwovalue 
  29.         }) 
  30.     }, 
  31.     methods: { 
  32.         ...mapMutations(['updateValue']) 
  33.     }, 
  34.     mounted() { 
  35.         this.updateValue('我是改变后的值:1'
  36.     }, 
  37. </script> 

我们看到两个模块的值都被改变了,为什么呢?因为VueX默认情况下,每个模块中的mutations都是在全局命名空间下的。那么我们肯定不希望这样。如果两个模块中的方法名不一样,当然不会出现这种情况,但是怎么才能避免这种情况呢?

我们需要定义一个属性namespaced为true。

  1. /* eslint-disable no-unused-vars */ 
  2. import Vue from 'vue' 
  3. import Vuex from 'vuex' 
  4.  
  5. Vue.use(Vuex) 
  6.  
  7. export default new Vuex.Store({ 
  8.   state:{ 
  9.     global:'this is global' 
  10.   }, 
  11.   modules: { 
  12.     moduleOne:{ 
  13.       namespaced:true, //在每个模块中定义为true,可以避免方法重名 
  14.       state:{ 
  15.         moduleOnevalue:'1' 
  16.       }, 
  17.       mutations:{ 
  18.         updateValue(state,value){ 
  19.           state.moduleOnevalue=value 
  20.         } 
  21.       } 
  22.       
  23.     }, 
  24.     moduleTwo:{ 
  25.       namespaced:true
  26.       state:{ 
  27.         moduleTwovalue:'0' 
  28.       }, 
  29.       mutations:{ 
  30.         updateValue(state,value){ 
  31.           state.moduleTwovalue=value 
  32.         } 
  33.       }  
  34.     } 
  35.   } 
  36. }) 

在页面中需要使用命名空间的方法调用它。

  1. <template> 
  2.     <div class="home"
  3.         <p>moduleOne_state:{{moduleOne}}</p> 
  4.         <p>moduleTwo_state:{{moduleTwo}}</p> 
  5.         <p>moduleOne_mapState:{{moduleOnevalue}}</p> 
  6.         <p>moduleTwo_mapState:{{moduleTwovalue}}</p> 
  7.     </div> 
  8. </template> 
  9. <script> 
  10. // eslint-disable-next-line no-unused-vars 
  11. import {mapState,mapMutations,mapGetters,mapActions} from 'vuex' 
  12. export default { 
  13.     name:"Home"
  14.     data() { 
  15.         return { 
  16.             msg:"this is Home" 
  17.         } 
  18.     }, 
  19.     computed: { 
  20.         moduleOne(){ 
  21.             return this.$store.state.moduleOne.moduleOnevalue 
  22.         }, 
  23.         moduleTwo(){ 
  24.             return this.$store.state.moduleTwo.moduleTwovalue 
  25.         }, 
  26.         ...mapState({ 
  27.            moduleOnevalue:(state)=>state.moduleOne.moduleOnevalue, 
  28.            moduleTwovalue:(state)=>state.moduleTwo.moduleTwovalue 
  29.         }) 
  30.     }, 
  31.     methods: { 
  32.         ...mapMutations(['moduleOne/updateValue','moduleTwo/updateValue']) 
  33.     }, 
  34.     mounted() { 
  35.         this['moduleOne/updateValue']('我是改变后的值:1'); 
  36.         this['moduleTwo/updateValue']('我是改变后的值:0'); 
  37.     }, 
  38. </script> 

四、在模块中添加getters

namespaced 同样在 getters也生效,下面我们在两个模块中定义了相同名字的方法。

  1. /* eslint-disable no-unused-vars */ 
  2. import Vue from 'vue' 
  3. import Vuex from 'vuex' 
  4.  
  5. Vue.use(Vuex) 
  6.  
  7. export default new Vuex.Store({ 
  8.   state:{ 
  9.     global:'this is global' 
  10.   }, 
  11.   modules: { 
  12.     moduleOne:{ 
  13.       namespaced:true
  14.       state:{ 
  15.         moduleOnevalue:'1' 
  16.       }, 
  17.       mutations:{ 
  18.         updateValue(state,value){ 
  19.           state.moduleOnevalue=value 
  20.         } 
  21.       }, 
  22.       getters:{ 
  23.         valuePlus(state){ 
  24.           return state.moduleOnevalue+'1' 
  25.         } 
  26.       }  
  27.       
  28.     }, 
  29.     moduleTwo:{ 
  30.       namespaced:true
  31.       state:{ 
  32.         moduleTwovalue:'0' 
  33.       }, 
  34.       mutations:{ 
  35.         updateValue(state,value){ 
  36.           state.moduleTwovalue=value 
  37.         } 
  38.       }, 
  39.       getters:{ 
  40.         valuePlus(state){ 
  41.           return state.moduleTwovalue+'0' 
  42.         } 
  43.       }  
  44.     } 
  45.   } 
  46. }) 

在页面引用查看效果。

  1. <template> 
  2.     <div class="home"
  3.         <p>moduleOne_state:{{moduleOne}}</p> 
  4.         <p>moduleTwo_state:{{moduleTwo}}</p> 
  5.         <p>moduleOne_mapState:{{moduleOnevalue}}</p> 
  6.         <p>moduleTwo_mapState:{{moduleTwovalue}}</p> 
  7.         <p>moduleOne_mapGetters:{{OnevaluePlus}}</p> 
  8.         <p>moduleTwo_mapGetters:{{TwovaluePlus}}</p> 
  9.     </div> 
  10. </template> 
  11. <script> 
  12. // eslint-disable-next-line no-unused-vars 
  13. import {mapState,mapMutations,mapGetters,mapActions} from 'vuex' 
  14. export default { 
  15.     name:"Home"
  16.     data() { 
  17.         return { 
  18.             msg:"this is Home" 
  19.         } 
  20.     }, 
  21.     computed: { 
  22.         moduleOne(){ 
  23.             return this.$store.state.moduleOne.moduleOnevalue 
  24.         }, 
  25.         moduleTwo(){ 
  26.             return this.$store.state.moduleTwo.moduleTwovalue 
  27.         }, 
  28.         ...mapState({ 
  29.            moduleOnevalue:(state)=>state.moduleOne.moduleOnevalue, 
  30.            moduleTwovalue:(state)=>state.moduleTwo.moduleTwovalue 
  31.         }), 
  32.         ...mapGetters({ 
  33.             OnevaluePlus:'moduleOne/valuePlus'
  34.             TwovaluePlus:'moduleTwo/valuePlus' 
  35.         }) 
  36.     }, 
  37.     methods: { 
  38.         ...mapMutations(['moduleOne/updateValue','moduleTwo/updateValue']) 
  39.     }, 
  40.     mounted() { 
  41.          // this['moduleOne/updateValue']('我是改变后的值:1'); 
  42.         // this['moduleTwo/updateValue']('我是改变后的值:0'); 
  43.     }, 
  44. </script> 

我们也可以获取全局的变量,第三个参数就是获取全局变量的参数。

  1. /* eslint-disable no-unused-vars */ 
  2. import Vue from 'vue' 
  3. import Vuex from 'vuex' 
  4.  
  5. Vue.use(Vuex) 
  6.  
  7. export default new Vuex.Store({ 
  8.   state:{ 
  9.     global:'this is global' 
  10.   }, 
  11.   modules: { 
  12.     moduleOne:{ 
  13.       namespaced:true
  14.       state:{ 
  15.         moduleOnevalue:'1' 
  16.       }, 
  17.       mutations:{ 
  18.         updateValue(state,value){ 
  19.           state.moduleOnevalue=value 
  20.         } 
  21.       }, 
  22.       getters:{ 
  23.         valuePlus(state){ 
  24.           return state.moduleOnevalue+'1' 
  25.         }, 
  26.         globalValuePlus(state,getters,rootState){ 
  27.           return state.moduleOnevalue+rootState.global 
  28.         } 
  29.       }  
  30.       
  31.     }, 
  32.     moduleTwo:{ 
  33.       namespaced:true
  34.       state:{ 
  35.         moduleTwovalue:'0' 
  36.       }, 
  37.       mutations:{ 
  38.         updateValue(state,value){ 
  39.           state.moduleTwovalue=value 
  40.         } 
  41.       }, 
  42.       getters:{ 
  43.         valuePlus(state){ 
  44.           return state.moduleTwovalue+'0' 
  45.         }, 
  46.         globalValuePlus(state,getters,rootState){ 
  47.           return state.moduleTwovalue+rootState.global 
  48.         } 
  49.       }  
  50.     } 
  51.   } 
  52. }) 

在页面查看。

  1. <template> 
  2.     <div class="home"
  3.         <p>moduleOne_state:{{moduleOne}}</p> 
  4.         <p>moduleTwo_state:{{moduleTwo}}</p> 
  5.         <p>moduleOne_mapState:{{moduleOnevalue}}</p> 
  6.         <p>moduleTwo_mapState:{{moduleTwovalue}}</p> 
  7.         <p>moduleOne_mapGetters:{{OnevaluePlus}}</p> 
  8.         <p>moduleTwo_mapGetters:{{TwovaluePlus}}</p> 
  9.         <p>moduleOne_mapGetters_global:{{OneglobalValue}}</p> 
  10.         <p>moduleTwo_mapGetters_global:{{TwoglobalValue}}</p> 
  11.     </div> 
  12. </template> 
  13. <script> 
  14. // eslint-disable-next-line no-unused-vars 
  15. import {mapState,mapMutations,mapGetters,mapActions} from 'vuex' 
  16. export default { 
  17.     name:"Home"
  18.     data() { 
  19.         return { 
  20.             msg:"this is Home" 
  21.         } 
  22.     }, 
  23.     computed: { 
  24.         moduleOne(){ 
  25.             return this.$store.state.moduleOne.moduleOnevalue 
  26.         }, 
  27.         moduleTwo(){ 
  28.             return this.$store.state.moduleTwo.moduleTwovalue 
  29.         }, 
  30.         ...mapState({ 
  31.            moduleOnevalue:(state)=>state.moduleOne.moduleOnevalue, 
  32.            moduleTwovalue:(state)=>state.moduleTwo.moduleTwovalue 
  33.         }), 
  34.         ...mapGetters({ 
  35.             OnevaluePlus:'moduleOne/valuePlus'
  36.             TwovaluePlus:'moduleTwo/valuePlus'
  37.             OneglobalValue:'moduleOne/globalValuePlus'
  38.             TwoglobalValue:'moduleTwo/globalValuePlus' 
  39.         }) 
  40.     }, 
  41.     methods: { 
  42.         ...mapMutations(['moduleOne/updateValue','moduleTwo/updateValue']) 
  43.     }, 
  44.     mounted() { 
  45.         // this['moduleOne/updateValue']('我是改变后的值:1'); 
  46.         // this['moduleTwo/updateValue']('我是改变后的值:0'); 
  47.     }, 
  48. </script> 

也可以获取其他模块的变量。

  1. /* eslint-disable no-unused-vars */ 
  2. import Vue from 'vue' 
  3. import Vuex from 'vuex' 
  4.  
  5. Vue.use(Vuex) 
  6.  
  7. export default new Vuex.Store({ 
  8.   state:{ 
  9.     global:'this is global' 
  10.   }, 
  11.   modules: { 
  12.     moduleOne:{ 
  13.       namespaced:true
  14.       state:{ 
  15.         moduleOnevalue:'1' 
  16.       }, 
  17.       mutations:{ 
  18.         updateValue(state,value){ 
  19.           state.moduleOnevalue=value 
  20.         } 
  21.       }, 
  22.       getters:{ 
  23.         valuePlus(state){ 
  24.           return state.moduleOnevalue+'1' 
  25.         }, 
  26.         globalValuePlus(state,getters,rootState){ 
  27.           return state.moduleOnevalue+rootState.global 
  28.         }, 
  29.         otherValuePlus(state,getters,rootState){ 
  30.           return state.moduleOnevalue+rootState.moduleTwo.moduleTwovalue 
  31.         }, 
  32.       }  
  33.       
  34.     }, 
  35.     moduleTwo:{ 
  36.       namespaced:true
  37.       state:{ 
  38.         moduleTwovalue:'0' 
  39.       }, 
  40.       mutations:{ 
  41.         updateValue(state,value){ 
  42.           state.moduleTwovalue=value 
  43.         } 
  44.       }, 
  45.       getters:{ 
  46.         valuePlus(state){ 
  47.           return state.moduleTwovalue+'0' 
  48.         }, 
  49.         globalValuePlus(state,getters,rootState){ 
  50.           return state.moduleTwovalue+rootState.global 
  51.         }, 
  52.         otherValuePlus(state,getters,rootState){ 
  53.           return state.moduleTwovalue+rootState.moduleOne.moduleOnevalue 
  54.         }, 
  55.       }  
  56.     } 
  57.   } 
  58. }) 

在页面查看。

  1. <template> 
  2.     <div class="home"
  3.         <p>moduleOne_state:{{moduleOne}}</p> 
  4.         <p>moduleTwo_state:{{moduleTwo}}</p> 
  5.         <p>moduleOne_mapState:{{moduleOnevalue}}</p> 
  6.         <p>moduleTwo_mapState:{{moduleTwovalue}}</p> 
  7.         <p>moduleOne_mapGetters:{{OnevaluePlus}}</p> 
  8.         <p>moduleTwo_mapGetters:{{TwovaluePlus}}</p> 
  9.         <p>moduleOne_mapGetters_global:{{OneglobalValue}}</p> 
  10.         <p>moduleTwo_mapGetters_global:{{TwoglobalValue}}</p> 
  11.         <p>moduleOne_mapGetters_other:{{OneotherValue}}</p> 
  12.         <p>moduleTwo_mapGetters_other:{{TwootherValue}}</p> 
  13.     </div> 
  14. </template> 
  15. <script> 
  16. // eslint-disable-next-line no-unused-vars 
  17. import {mapState,mapMutations,mapGetters,mapActions} from 'vuex' 
  18. export default { 
  19.     name:"Home"
  20.     data() { 
  21.         return { 
  22.             msg:"this is Home" 
  23.         } 
  24.     }, 
  25.     computed: { 
  26.         moduleOne(){ 
  27.             return this.$store.state.moduleOne.moduleOnevalue 
  28.         }, 
  29.         moduleTwo(){ 
  30.             return this.$store.state.moduleTwo.moduleTwovalue 
  31.         }, 
  32.         ...mapState({ 
  33.            moduleOnevalue:(state)=>state.moduleOne.moduleOnevalue, 
  34.            moduleTwovalue:(state)=>state.moduleTwo.moduleTwovalue 
  35.         }), 
  36.         ...mapGetters({ 
  37.             OnevaluePlus:'moduleOne/valuePlus'
  38.             TwovaluePlus:'moduleTwo/valuePlus'
  39.             OneglobalValue:'moduleOne/globalValuePlus'
  40.             TwoglobalValue:'moduleTwo/globalValuePlus'
  41.             OneotherValue:'moduleOne/otherValuePlus'
  42.             TwootherValue:'moduleTwo/otherValuePlus' 
  43.         }) 
  44.     }, 
  45.     methods: { 
  46.         ...mapMutations(['moduleOne/updateValue','moduleTwo/updateValue']) 
  47.     }, 
  48.     mounted() { 
  49.         // this['moduleOne/updateValue']('我是改变后的值:1'); 
  50.         // this['moduleTwo/updateValue']('我是改变后的值:0'); 
  51.     }, 
  52. </script> 

 

五、在模块中添加actions

actions对象中的方法有一个参数对象ctx。里面分别{state,commit,rootState}。这里我们直接展开用。actions默认只会提交本地模块中的mutations。如果需要提交全局或者其他模块,需要在commit方法的第三个参数上加上{root:true}。

  1. /* eslint-disable no-unused-vars */ 
  2. import Vue from 'vue' 
  3. import Vuex from 'vuex' 
  4.  
  5. Vue.use(Vuex) 
  6.  
  7. export default new Vuex.Store({ 
  8.   state:{ 
  9.     global:'this is global' 
  10.   }, 
  11.   modules: { 
  12.     moduleOne:{ 
  13.       namespaced:true
  14.       state:{ 
  15.         moduleOnevalue:'1' 
  16.       }, 
  17.       mutations:{ 
  18.         updateValue(state,value){ 
  19.           state.moduleOnevalue=value 
  20.         } 
  21.       }, 
  22.       getters:{ 
  23.         valuePlus(state){ 
  24.           return state.moduleOnevalue+'1' 
  25.         }, 
  26.         globalValuePlus(state,getters,rootState){ 
  27.           return state.moduleOnevalue+rootState.global 
  28.         }, 
  29.         otherValuePlus(state,getters,rootState){ 
  30.           return state.moduleOnevalue+rootState.moduleTwo.moduleTwovalue 
  31.         }, 
  32.       }, 
  33.       actions:{ 
  34.         timeOut({state,commit,rootState}){ 
  35.           setTimeout(()=>{ 
  36.             commit('updateValue','我是异步改变的值:1'
  37.           },3000) 
  38.         } 
  39.       }  
  40.       
  41.     }, 
  42.     moduleTwo:{ 
  43.       namespaced:true
  44.       state:{ 
  45.         moduleTwovalue:'0' 
  46.       }, 
  47.       mutations:{ 
  48.         updateValue(state,value){ 
  49.           state.moduleTwovalue=value 
  50.         } 
  51.       }, 
  52.       getters:{ 
  53.         valuePlus(state){ 
  54.           return state.moduleTwovalue+'0' 
  55.         }, 
  56.         globalValuePlus(state,getters,rootState){ 
  57.           return state.moduleTwovalue+rootState.global 
  58.         }, 
  59.         otherValuePlus(state,getters,rootState){ 
  60.           return state.moduleTwovalue+rootState.moduleOne.moduleOnevalue 
  61.         }, 
  62.       }  
  63.     } 
  64.   } 
  65. }) 

页面引用。

  1. <template> 
  2.     <div class="home"
  3.         <p>moduleOne_state:{{moduleOne}}</p> 
  4.         <p>moduleTwo_state:{{moduleTwo}}</p> 
  5.         <p>moduleOne_mapState:{{moduleOnevalue}}</p> 
  6.         <p>moduleTwo_mapState:{{moduleTwovalue}}</p> 
  7.         <p>moduleOne_mapGetters:{{OnevaluePlus}}</p> 
  8.         <p>moduleTwo_mapGetters:{{TwovaluePlus}}</p> 
  9.         <p>moduleOne_mapGetters_global:{{OneglobalValue}}</p> 
  10.         <p>moduleTwo_mapGetters_global:{{TwoglobalValue}}</p> 
  11.         <p>moduleOne_mapGetters_other:{{OneotherValue}}</p> 
  12.         <p>moduleTwo_mapGetters_other:{{TwootherValue}}</p> 
  13.     </div> 
  14. </template> 
  15. <script> 
  16. // eslint-disable-next-line no-unused-vars 
  17. import {mapState,mapMutations,mapGetters,mapActions} from 'vuex' 
  18. export default { 
  19.     name:"Home"
  20.     data() { 
  21.         return { 
  22.             msg:"this is Home" 
  23.         } 
  24.     }, 
  25.     computed: { 
  26.         moduleOne(){ 
  27.             return this.$store.state.moduleOne.moduleOnevalue 
  28.         }, 
  29.         moduleTwo(){ 
  30.             return this.$store.state.moduleTwo.moduleTwovalue 
  31.         }, 
  32.         ...mapState({ 
  33.            moduleOnevalue:(state)=>state.moduleOne.moduleOnevalue, 
  34.            moduleTwovalue:(state)=>state.moduleTwo.moduleTwovalue 
  35.         }), 
  36.         ...mapGetters({ 
  37.             OnevaluePlus:'moduleOne/valuePlus'
  38.             TwovaluePlus:'moduleTwo/valuePlus'
  39.             OneglobalValue:'moduleOne/globalValuePlus'
  40.             TwoglobalValue:'moduleTwo/globalValuePlus'
  41.             OneotherValue:'moduleOne/otherValuePlus'
  42.             TwootherValue:'moduleTwo/otherValuePlus' 
  43.         }) 
  44.     }, 
  45.     methods: { 
  46.         ...mapMutations(['moduleOne/updateValue','moduleTwo/updateValue']), 
  47.         ...mapActions(['moduleOne/timeOut']) 
  48.     }, 
  49.     mounted() { 
  50.         // this['moduleOne/updateValue']('我是改变后的值:1'); 
  51.         // this['moduleTwo/updateValue']('我是改变后的值:0'); 
  52.         this['moduleOne/timeOut'](); 
  53.     }, 
  54. </script> 

下面我们看下如何提交全局或者其他模块的mutations。

  1. /* eslint-disable no-unused-vars */ 
  2. import Vue from 'vue' 
  3. import Vuex from 'vuex' 
  4.  
  5. Vue.use(Vuex) 
  6.  
  7. export default new Vuex.Store({ 
  8.   state:{ 
  9.     global:'this is global' 
  10.   }, 
  11.   mutations:{ 
  12.     mode(state,data){ 
  13.       state.global=data 
  14.     } 
  15.   }, 
  16.   modules: { 
  17.     moduleOne:{ 
  18.       namespaced:true
  19.       state:{ 
  20.         moduleOnevalue:'1' 
  21.       }, 
  22.       mutations:{ 
  23.         updateValue(state,value){ 
  24.           state.moduleOnevalue=value 
  25.         } 
  26.       }, 
  27.       getters:{ 
  28.         valuePlus(state){ 
  29.           return state.moduleOnevalue+'1' 
  30.         }, 
  31.         globalValuePlus(state,getters,rootState){ 
  32.           return state.moduleOnevalue+rootState.global 
  33.         }, 
  34.         otherValuePlus(state,getters,rootState){ 
  35.           return state.moduleOnevalue+rootState.moduleTwo.moduleTwovalue 
  36.         }, 
  37.       }, 
  38.       actions:{ 
  39.         timeOut({state,commit,rootState}){ 
  40.           setTimeout(()=>{ 
  41.             commit('updateValue','我是异步改变的值:1'
  42.           },3000) 
  43.         }, 
  44.         globaltimeOut({commit}){ 
  45.           setTimeout(()=>{ 
  46.             commit('mode','我改变了global的值',{root:true}) 
  47.           },3000) 
  48.         } 
  49.       }  
  50.       
  51.     }, 
  52.     moduleTwo:{ 
  53.       namespaced:true
  54.       state:{ 
  55.         moduleTwovalue:'0' 
  56.       }, 
  57.       mutations:{ 
  58.         updateValue(state,value){ 
  59.           state.moduleTwovalue=value 
  60.         } 
  61.       }, 
  62.       getters:{ 
  63.         valuePlus(state){ 
  64.           return state.moduleTwovalue+'0' 
  65.         }, 
  66.         globalValuePlus(state,getters,rootState){ 
  67.           return state.moduleTwovalue+rootState.global 
  68.         }, 
  69.         otherValuePlus(state,getters,rootState){ 
  70.           return state.moduleTwovalue+rootState.moduleOne.moduleOnevalue 
  71.         }, 
  72.       }  
  73.     } 
  74.   } 
  75. }) 

页面引用。

  1. <template> 
  2.     <div class="home"
  3.         <p>moduleOne_state:{{moduleOne}}</p> 
  4.         <p>moduleTwo_state:{{moduleTwo}}</p> 
  5.         <p>moduleOne_mapState:{{moduleOnevalue}}</p> 
  6.         <p>moduleTwo_mapState:{{moduleTwovalue}}</p> 
  7.         <p>moduleOne_mapGetters:{{OnevaluePlus}}</p> 
  8.         <p>moduleTwo_mapGetters:{{TwovaluePlus}}</p> 
  9.         <p>moduleOne_mapGetters_global:{{OneglobalValue}}</p> 
  10.         <p>moduleTwo_mapGetters_global:{{TwoglobalValue}}</p> 
  11.         <p>moduleOne_mapGetters_other:{{OneotherValue}}</p> 
  12.         <p>moduleTwo_mapGetters_other:{{TwootherValue}}</p> 
  13.     </div> 
  14. </template> 
  15. <script> 
  16. // eslint-disable-next-line no-unused-vars 
  17. import {mapState,mapMutations,mapGetters,mapActions} from 'vuex' 
  18. export default { 
  19.     name:"Home"
  20.     data() { 
  21.         return { 
  22.             msg:"this is Home" 
  23.         } 
  24.     }, 
  25.     computed: { 
  26.         moduleOne(){ 
  27.             return this.$store.state.moduleOne.moduleOnevalue 
  28.         }, 
  29.         moduleTwo(){ 
  30.             return this.$store.state.moduleTwo.moduleTwovalue 
  31.         }, 
  32.         ...mapState({ 
  33.            moduleOnevalue:(state)=>state.moduleOne.moduleOnevalue, 
  34.            moduleTwovalue:(state)=>state.moduleTwo.moduleTwovalue 
  35.         }), 
  36.         ...mapGetters({ 
  37.             OnevaluePlus:'moduleOne/valuePlus'
  38.             TwovaluePlus:'moduleTwo/valuePlus'
  39.             OneglobalValue:'moduleOne/globalValuePlus'
  40.             TwoglobalValue:'moduleTwo/globalValuePlus'
  41.             OneotherValue:'moduleOne/otherValuePlus'
  42.             TwootherValue:'moduleTwo/otherValuePlus' 
  43.         }) 
  44.     }, 
  45.     methods: { 
  46.         ...mapMutations(['moduleOne/updateValue','moduleTwo/updateValue']), 
  47.         ...mapActions(['moduleOne/timeOut','moduleOne/globaltimeOut']) 
  48.     }, 
  49.     mounted() { 
  50.         // this['moduleOne/updateValue']('我是改变后的值:1'); 
  51.         // this['moduleTwo/updateValue']('我是改变后的值:0'); 
  52.         // this['moduleOne/timeOut'](); 
  53.         this['moduleOne/globaltimeOut'](); 
  54.     }, 
  55. </script> 

那么提交其他模块的呢?

  1. /* eslint-disable no-unused-vars */ 
  2. import Vue from 'vue' 
  3. import Vuex from 'vuex' 
  4.  
  5. Vue.use(Vuex) 
  6.  
  7. export default new Vuex.Store({ 
  8.   state:{ 
  9.     global:'this is global' 
  10.   }, 
  11.   mutations:{ 
  12.     mode(state,data){ 
  13.       state.global=data 
  14.     } 
  15.   }, 
  16.   modules: { 
  17.     moduleOne:{ 
  18.       namespaced:true
  19.       state:{ 
  20.         moduleOnevalue:'1' 
  21.       }, 
  22.       mutations:{ 
  23.         updateValue(state,value){ 
  24.           state.moduleOnevalue=value 
  25.         } 
  26.       }, 
  27.       getters:{ 
  28.         valuePlus(state){ 
  29.           return state.moduleOnevalue+'1' 
  30.         }, 
  31.         globalValuePlus(state,getters,rootState){ 
  32.           return state.moduleOnevalue+rootState.global 
  33.         }, 
  34.         otherValuePlus(state,getters,rootState){ 
  35.           return state.moduleOnevalue+rootState.moduleTwo.moduleTwovalue 
  36.         }, 
  37.       }, 
  38.       actions:{ 
  39.         timeOut({state,commit,rootState}){ 
  40.           setTimeout(()=>{ 
  41.             commit('updateValue','我是异步改变的值:1'
  42.           },3000) 
  43.         }, 
  44.         globaltimeOut({commit}){ 
  45.           setTimeout(()=>{ 
  46.             commit('mode','我改变了global的值',{root:true}) 
  47.           },3000) 
  48.         }, 
  49.         othertimeOut({commit}){ 
  50.           setTimeout(()=>{ 
  51.             commit('moduleTwo/updateValue','我改变了moduleTwo的值',{root:true}) 
  52.           },3000) 
  53.         } 
  54.       }  
  55.       
  56.     }, 
  57.     moduleTwo:{ 
  58.       namespaced:true
  59.       state:{ 
  60.         moduleTwovalue:'0' 
  61.       }, 
  62.       mutations:{ 
  63.         updateValue(state,value){ 
  64.           state.moduleTwovalue=value 
  65.         } 
  66.       }, 
  67.       getters:{ 
  68.         valuePlus(state){ 
  69.           return state.moduleTwovalue+'0' 
  70.         }, 
  71.         globalValuePlus(state,getters,rootState){ 
  72.           return state.moduleTwovalue+rootState.global 
  73.         }, 
  74.         otherValuePlus(state,getters,rootState){ 
  75.           return state.moduleTwovalue+rootState.moduleOne.moduleOnevalue 
  76.         }, 
  77.       }  
  78.     } 
  79.   } 
  80. }) 

页面引用。

  1. <template> 
  2.     <div class="home"
  3.         <p>moduleOne_state:{{moduleOne}}</p> 
  4.         <p>moduleTwo_state:{{moduleTwo}}</p> 
  5.         <p>moduleOne_mapState:{{moduleOnevalue}}</p> 
  6.         <p>moduleTwo_mapState:{{moduleTwovalue}}</p> 
  7.         <p>moduleOne_mapGetters:{{OnevaluePlus}}</p> 
  8.         <p>moduleTwo_mapGetters:{{TwovaluePlus}}</p> 
  9.         <p>moduleOne_mapGetters_global:{{OneglobalValue}}</p> 
  10.         <p>moduleTwo_mapGetters_global:{{TwoglobalValue}}</p> 
  11.         <p>moduleOne_mapGetters_other:{{OneotherValue}}</p> 
  12.         <p>moduleTwo_mapGetters_other:{{TwootherValue}}</p> 
  13.     </div> 
  14. </template> 
  15. <script> 
  16. // eslint-disable-next-line no-unused-vars 
  17. import {mapState,mapMutations,mapGetters,mapActions} from 'vuex' 
  18. export default { 
  19.     name:"Home"
  20.     data() { 
  21.         return { 
  22.             msg:"this is Home" 
  23.         } 
  24.     }, 
  25.     computed: { 
  26.         moduleOne(){ 
  27.             return this.$store.state.moduleOne.moduleOnevalue 
  28.         }, 
  29.         moduleTwo(){ 
  30.             return this.$store.state.moduleTwo.moduleTwovalue 
  31.         }, 
  32.         ...mapState({ 
  33.            moduleOnevalue:(state)=>state.moduleOne.moduleOnevalue, 
  34.            moduleTwovalue:(state)=>state.moduleTwo.moduleTwovalue 
  35.         }), 
  36.         ...mapGetters({ 
  37.             OnevaluePlus:'moduleOne/valuePlus'
  38.             TwovaluePlus:'moduleTwo/valuePlus'
  39.             OneglobalValue:'moduleOne/globalValuePlus'
  40.             TwoglobalValue:'moduleTwo/globalValuePlus'
  41.             OneotherValue:'moduleOne/otherValuePlus'
  42.             TwootherValue:'moduleTwo/otherValuePlus' 
  43.         }) 
  44.     }, 
  45.     methods: { 
  46.         ...mapMutations(['moduleOne/updateValue','moduleTwo/updateValue']), 
  47.         ...mapActions(['moduleOne/timeOut','moduleOne/globaltimeOut','moduleOne/othertimeOut']) 
  48.     }, 
  49.     mounted() { 
  50.         // this['moduleOne/updateValue']('我是改变后的值:1'); 
  51.         // this['moduleTwo/updateValue']('我是改变后的值:0'); 
  52.         // this['moduleOne/timeOut'](); 
  53.         // this['moduleOne/globaltimeOut'](); 
  54.         this['moduleOne/othertimeOut'](); 
  55.     }, 
  56. </script> 

注意:你可以在module中再继续添加模块,可以无限循环下去。

六、动态注册模块

有时候,我们会使用router的异步加载路由,有些地方会用不到一些模块的数据,那么我们利用VueX的动态注册模块。我们来到入口文件main.js中。

  1. import Vue from 'vue' 
  2. import App from './App.vue' 
  3. import router from './router' 
  4. import store from './store' 
  5.  
  6. Vue.config.productionTip = false 
  7. // 动态注册模块 
  8. store.registerModule('moduleThree',{ 
  9.   state:{ 
  10.     text:"this is moduleThree" 
  11.   } 
  12. }) 
  13.  
  14. new Vue({ 
  15.   router, 
  16.   store, 
  17.   render: h => h(App) 
  18. }).$mount('#app'

在页面引用它。

  1. <template> 
  2.     <div class="home"
  3.         <p>moduleOne_state:{{moduleOne}}</p> 
  4.         <p>moduleTwo_state:{{moduleTwo}}</p> 
  5.         <p>moduleOne_mapState:{{moduleOnevalue}}</p> 
  6.         <p>moduleTwo_mapState:{{moduleTwovalue}}</p> 
  7.         <p>moduleOne_mapGetters:{{OnevaluePlus}}</p> 
  8.         <p>moduleTwo_mapGetters:{{TwovaluePlus}}</p> 
  9.         <p>moduleOne_mapGetters_global:{{OneglobalValue}}</p> 
  10.         <p>moduleTwo_mapGetters_global:{{TwoglobalValue}}</p> 
  11.         <p>moduleOne_mapGetters_other:{{OneotherValue}}</p> 
  12.         <p>moduleTwo_mapGetters_other:{{TwootherValue}}</p> 
  13.         <p>moduleThree_mapState:{{moduleThreetext}}</p> 
  14.     </div> 
  15. </template> 
  16. <script> 
  17. // eslint-disable-next-line no-unused-vars 
  18. import {mapState,mapMutations,mapGetters,mapActions} from 'vuex' 
  19. export default { 
  20.     name:"Home"
  21.     data() { 
  22.         return { 
  23.             msg:"this is Home" 
  24.         } 
  25.     }, 
  26.     computed: { 
  27.         moduleOne(){ 
  28.             return this.$store.state.moduleOne.moduleOnevalue 
  29.         }, 
  30.         moduleTwo(){ 
  31.             return this.$store.state.moduleTwo.moduleTwovalue 
  32.         }, 
  33.         ...mapState({ 
  34.            moduleOnevalue:(state)=>state.moduleOne.moduleOnevalue, 
  35.            moduleTwovalue:(state)=>state.moduleTwo.moduleTwovalue, 
  36.            moduleThreetext:(state)=>state.moduleThree.text 
  37.         }), 
  38.         ...mapGetters({ 
  39.             OnevaluePlus:'moduleOne/valuePlus'
  40.             TwovaluePlus:'moduleTwo/valuePlus'
  41.             OneglobalValue:'moduleOne/globalValuePlus'
  42.             TwoglobalValue:'moduleTwo/globalValuePlus'
  43.             OneotherValue:'moduleOne/otherValuePlus'
  44.             TwootherValue:'moduleTwo/otherValuePlus' 
  45.         }) 
  46.     }, 
  47.     methods: { 
  48.         ...mapMutations(['moduleOne/updateValue','moduleTwo/updateValue']), 
  49.         ...mapActions(['moduleOne/timeOut','moduleOne/globaltimeOut','moduleOne/othertimeOut']) 
  50.     }, 
  51.     mounted() { 
  52.         // this['moduleOne/updateValue']('我是改变后的值:1'); 
  53.         // this['moduleTwo/updateValue']('我是改变后的值:0'); 
  54.         // this['moduleOne/timeOut'](); 
  55.         // this['moduleOne/globaltimeOut'](); 
  56.         // this['moduleOne/othertimeOut'](); 
  57.     }, 
  58. </script> 

本文转载自微信公众号「前端历劫之路」,可以通过以下二维码关注。转载本文请联系前端历劫之路公众号。

 

责任编辑:武晓燕 来源: 前端历劫之路
相关推荐

2017-09-26 11:43:12

Java异常和处理

2023-09-18 08:56:57

StringJava

2022-08-11 08:46:23

索引数据结构

2018-01-02 09:31:12

大数据数据互联网

2019-05-08 16:00:48

人工智能人脸识别刷脸

2023-08-28 07:39:49

线程调度基本单位

2022-03-23 15:36:13

数字化转型数据治理企业

2023-08-02 08:14:33

监控MTS性能

2020-12-30 10:17:06

运营商5G互联网

2015-11-02 09:50:48

电脑1秒钟事情

2018-08-24 06:56:17

2011-06-24 17:30:34

网站权重

2023-03-23 08:11:59

2022-04-02 14:51:58

数据中心数据安全物联网

2024-04-17 08:35:04

Lua脚本Redis数据结构

2022-01-13 15:31:14

Redis持久化配置

2024-03-20 00:04:46

TypeScriptas const类型断言

2019-07-04 05:22:02

物联网设备物联网IOT

2022-06-29 08:32:04

游标MySQL服务器

2023-05-06 08:53:13

点赞
收藏

51CTO技术栈公众号