vue modules 使用

由于使用单一状态树,应用的所有状态会集中到一个比较大的对象。当应用变得非常复杂时,store 对象就有可能变得相当臃肿。
为了解决以上问题,Vuex 允许我们将 store 分割成模块(module)。每个模块拥有自己的 state、mutation、action、getter、甚至是嵌套子模块
const moduleA = {
  state: () => ({ ... }),
  mutations: { ... },
  actions: { ... },
  getters: { ... }
}

const moduleB = {
  state: () => ({ ... }),
  mutations: { ... },
  actions: { ... }
}

const store = new Vuex.Store({
  modules: {
    a: moduleA,
    b: moduleB
  }
})

store.state.a // -> moduleA 的状态
store.state.b // -> moduleB 的状态
  1. 如何在.vue文件中使用modules呢?
    方法一:
    以提交mutation为例:
this.$store.commit('module1/set_xxx', {data: xxx});

但是这样写每次都要写具体的module,未免会比较繁琐
方法二:使用mapGetters、mapMutations、mapActions
同样以提交mutation为例
首先import mapMutation

import {mapMutations} from 'vuex';

然后在method中

// 写法1
methods: {
    ...mapMutations('module1', ['set_xxx'])
}
this['set_xxx'](data);

// 写法2
methods: {
     ...mapMutations({
            set_test: 'module1/set_xxx'
        })
}
this['set_test'](data)

同样mapGetters也使用同样的方式

...mapGetters('module1',['data1','data1'])
cosole.log(this.data1);
console.log(this.data2); 

 

参考文章

https://vuex.vuejs.org/zh/guide/modules.html
https://juejin.im/post/5c997e59f265da60f6731774



作者:cc要坚定自己的脚步
链接:https://www.jianshu.com/p/85879bc588ae
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

 

这篇文章主要介绍了vuex中modules的基本用法。

1. store文件结构

- src
- components
- store
    -index.js
    -modules
        -app.js
        -bus.js

2.1index.js中-手动引入modules

import Vue from 'vue'
import Vuex from 'vuex'

import bus from './module/bus'
import app from './module/app'

Vue.use(Vuex)
export default new Vuex.Store({
    state: {
        // 这里是根vuex状态
    },
    mutations: {
        // 这里是根vuex状态
    },
    actions: {
        // 这里是根vuex状态
    },
    modules: { // 子vuex状态模块注册
        namespaced: true, // 为了解决不同模块命名冲突的问题
        app,
        bus
    }
})

2.2 index.js中-动态引入modules

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const dynamicModules = {}
const files = require.context('.', true, /\.js$/)

const dynamicImportModules = (modules, file, splits, index = 0) => {
    if (index == 0 && splits[0] == 'modules') {
        ++index
    }
    if (splits.length == index + 1) {
        if ('index' == splits[index]) {
            modules[splits[index - 1]] = files(file).default
        } else {
            modules.modules[splits[index]] = files(file).default
        }
    } else {
        let tmpModules = {}
        if ('index' == splits[index + 1]) {
            tmpModules = modules
        } else {
            modules[splits[index]] = modules[splits[index]] ? modules[splits[index]] : {namespaced: true, modules: {}}
            tmpModules = modules[splits[index]]
        }
        dynamicImportModules(tmpModules, file, splits, ++index)
    }
}

files.keys().filter(file => file != './index.js').forEach(file => {
    let splits = file.replace(/(\.\/|\.js)/g, '').split('\/');
    dynamicImportModules(dynamicModules, file, splits)
})

export default new Vuex.Store({
    modules: dynamicModules,
    strict: process.env.NODE_ENV !== 'production'
})

app.js文件内容

const state = {
    user: {}, // 需要管理的状态数据
}

const mutations = {
    setUser (state, val) {
            state.user = val
        }
}
const getters = {}
const actions = {}

export default {
    namespaced: true,
    state,
    mutations,
    getters,
    actions
}

4 .1 使用 a.vue页面

// 使用模块中的mutations、getters、actions时候,要加上模块名,例如使用commint执行mutations时
// 格式: 模块名/模块中的mutations
this.$store.commit("app/setUser", user)

// 获取属性时同样加上模块名
this.$store.state.app.user 

4.2 utils.js中使用

// 引入 这里的store 就相当于页面中的 this.$store
import store from '@/store'

export const setCurUser = (user) => {
    let curUser = store.app.user
    if(!curUser) {
        store.commit("app/setUser", user)
        return user
    }
    
    return curUser
}

5 配置main.js

import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'

new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
https://www.cnblogs.com/codebook/p/13416465.html
posted @ 2022-05-03 11:45  技术颜良  阅读(772)  评论(0编辑  收藏  举报