Vuex 之 module 使用方法及场景

                                vuex 之 module 使用方法及场景

 

一.  module 使用场景

       在项目开发过程中,随着项目逐渐增大,数据关联复杂度逐渐加大, 多人协同开发,人员变动等。 我们会遇到vuex数据更新时,执行某个action 导致同名/未预测到的关联数据发生了变化。 

      vue 基本思想之一便是数据驱动, vuex 更是专门的数据状态关联库。 导致数据错误结果可想而知......

      使用vuex module 命名空间概念则可以很好的解决这个问题!!!

 

二.  实例演练

      先贴个demo

     store.js

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const test1 = {
  namespaced: true,
  state: {
    name: 'moduleA',
    type: 'module A'
  },
  mutations: {
    updateNameByMutation(state, appendStr){
      state.name = state.name + " append Str: " + appendStr
    }
  },
  actions: {
    udpateNameByAction({commit}, appendStr) {
      commit("updateNameByMutation", appendStr)
    }
  },
  getters: {
    getNameA(state){
      return state.name
    }
  }
}
const test2 = {
  // 当namespaced=true 时, vuex, 将会自动给各自module 添加访问路径名。 方便区分moduel
  namespaced: true,
  state:{
    name: 'moduleB',
    type: 'module B'
  },
  mutations: {
    updateNameByMutation(state, appendStr){
      state.name = state.name + " append Str: " + appendStr
    }
  },
  actions: {
    // 如果不使用命名空间, 那么view 指向actions 的该方法时,会执行所有与指定action名相同的函数(即:这里module A,B 中该action都会执行)
    udpateNameByAction({commit}, appendStr){
      commit("updateNameByMutation", appendStr)
    }
  },
  getters: {
    getNameB(state){
      return state.name
    }
  }
}

const storeInstall =  new Vuex.Store({
   state: {
     name: 'i am root state name'
   },
   modules:{
    // 这里的路径名: test1, test2, 在view 中 通过 mapActions('test1', [actionName]) 使用并区分需要使用的module
    test1,
    test2
   }
})

export default storeInstall

 store.js 几个简单的vuex 使用场景模拟。 我们有多个模块,分别为: test1, test2...   。 

       我们发现开发中可能会存在相同的stateName/ actionName/ mutaionName /。  (实际开发中,getterName 如果有重名编译会提示 getter 重名....)

      我们使用vuex 需要实例化一个Vuex的Store构造函数。 这里storeInstall 中第一个state, 我们可以理解为根 state, 它全局可访问。 modules 中则是我们自定义注册的module. 每个module 中都有自己独立的state, action, mutation, getter...  

      需要注意的是,这里通过给每个module 对象添加namespaced: true, 来达到命名空间来区分Module的效果。也是通过它来区分更新/调用 对应的vuex 方法来隔离未知数据更新等数据相关问题。

 

Test.vue

<template>
  <div>
    <div>
    <h2>Page Test1</h2>
    </div>
    <div>
    <a href="javascript:" @click="changeName">udpate: 名称Name</a>  &nbsp; &nbsp;
    <a href="javascript:" @click="showName">显示更新后的Name</a> &nbsp; &nbsp;
    </div>
  </div>
</template>
<script>
import { mapState, mapActions } from 'vuex'
export default {
  data(){
    return {}
  },
  computed: {
    ...mapState('test1', {
      state: state => state
    })
  },
  methods: {
    // test1 模块路径名
    ...mapActions('test1', [
      'udpateNameByAction'
    ]),
    changeName(){
      this["udpateNameByAction"]('ha ha test1 udpate !!')
    },
    showName(){
      console.log(this.$store.state)
    },
  },
  mounted() {
    console.log("store name: ", this.$store)
    console.log("namespace test1 state: ", this.state)
  }
}
</script>

         这个test1.vue (还有另外一个对应的test2.vue 代码基本一样,主要用来区别两个页面vuex的更新效果), 则向我们展示了如何使用vuex 提供的api 来做数据更新(这里介绍的是引入命名空间的module 场景)。

         (1) 首先通过vuex 引入需要的api 这里主要演示 ...mapActions .

         (2)  在module场景下引入mapActions 我们发现,第一个参数传的是 module 路径(就引入各个module的名称)名。 这种方式将只会在当前view中,导出指定模块下注册的 action 集合。

         (3)  当调用指定定module下的action 执行state 更新操作时, vuex 通过该module名找到对一定的action 进行下一步mutation 操作。 同时受到影响state的也只会时该命名空间下的state 

 

三.  Vuex module namespaced

      我们来讨论下module 设置了namespaced 与不设置vuex store 中的module数据有何区别?

      打字累截个图吧,一目了然:

      图1: 没有设置namespaced=true 

      

          图2: 设置namespaced=true 

     

       

    

四. 总结

     关于vuex module 这里只是个基本讲解。 总结下来就是module  给了我们一种隔离vuex store 各个 state及相关api 的方法,让数据相关操作在复杂的项目场景可以更清晰,易追踪。 

      

    

 

 

 

 

  • 12
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vuex 是一个专门为 Vue.js 应用程序开发的状态管理模式,它集中管理了应用程序的所有组件的状态,使得状态的变更变得可追踪和可控。Vuex 的核心是五个属性,分别是: 1. state:状态存储对象,用于存储应用程序的所有状态数据。 2. getters:计算属性,用于从 state 中派生出一些状态,可以当做 state 的计算属性使用。 3. mutations:同步修改状态的方法,用于修改 state 中的数据,必须是同步函数。 4. actions:异步修改状态的方法,用于提交 mutations,可以包含任意异步操作。 5. modules:用于将 store 拆分成模块,每个模块都有自己的 state、getters、mutations、actions 属性。 使用 Vuex 的步骤如下: 1. 安装 Vuex:`npm install vuex --save` 2. 在 main.js 中引入 Vuex:`import Vuex from 'vuex'` 3. 创建 store 实例:`const store = new Vuex.Store({ state, getters, mutations, actions, modules })` 4. 在 Vue 根实例中注入 store:`new Vue({ store, render: h => h(App) }).$mount('#app')` 5. 在组件中使用 state 和 getters:`this.$store.state.xxx` 和 `this.$store.getters.xxx` 6. 在组件中使用 mutations 和 actions:`this.$store.commit('xxx')` 和 `this.$store.dispatch('xxx')` 7. 在组件中使用模块中的 state、getters、mutations 和 actions:`this.$store.state.module.xxx`、`this.$store.getters.module.xxx`、`this.$store.commit('module/xxx')` 和 `this.$store.dispatch('module/xxx')`。 以上就是 Vuex 的五个属性及使用方法
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值