vuex 多个模块应用

 

index.js

import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'

Vue.use(Vuex)


const modulesFiles = require.context('./modules', true, /\.js$/)

const modules = modulesFiles.keys().reduce((modules, modulePath) => {
  // set './app.js' => 'app'
  const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
  const value = modulesFiles(modulePath)
  modules[moduleName] = value.default
  return modules
}, {})
console.log(modules)
const store = new Vuex.Store({
  modules,
  getters
})

export default store
app.js

const state = {
  language: getLanguage()
}

const mutations = {
   SET_LANGUAGE: (state, language) => {
    state.language = language
    Cookies.set('language', language)
  },
}

const actions = {
   setLanguage({ commit }, language) {
      commit('SET_LANGUAGE', language)
   },
}

export default {
  namespaced: true,  //重点
  state, 
  mutations,
  actions
}

 

传递的参数是:模块名字+actions的方法名

其他页面调用app.js 模块的action 方法通过 this.$store.dispatch('app/setLanguage', lang)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vuex 是一个用于 Vue.js 应用程序的状态管理库。在大型 Vue.js 应用程序中,随着应用程序的增长,状态管理会变得更加复杂。Vuex 提供了一种集中式存储管理应用程序中的所有组件的状态,使得状态管理变得简单和可维护。 在 Vuex 中,我们可以将状态分成多个模块。每个模块都有自己的 state、mutations、actions、getters 等属性,这些属性都是独立的。如果我们需要在多个模块中使用相同的代码,就需要编写辅助函数来简化代码。 以下是一些常用的 Vuex模块辅助函数: #### 1. mapState `mapState` 函数用于将 store 中的状态映射到组件的计算属性中。它接受一个数组或对象作为参数,数组中的每个元素都是一个字符串,表示 store 中的状态名称。对象中的每个属性名表示计算属性的名称,属性值表示 store 中的状态名称。 示例: ```javascript import { mapState } from 'vuex' export default { computed: { ...mapState({ count: 'counter' }) } } ``` #### 2. mapGetters `mapGetters` 函数用于将 store 中的 getters 映射到组件的计算属性中。它接受一个数组或对象作为参数,数组中的每个元素都是一个字符串,表示 store 中的 getter 名称。对象中的每个属性名表示计算属性的名称,属性值表示 store 中的 getter 名称。 示例: ```javascript import { mapGetters } from 'vuex' export default { computed: { ...mapGetters([ 'doneTodosCount', 'anotherGetter' ]) } } ``` #### 3. mapMutations `mapMutations` 函数用于将 store 中的 mutations 映射到组件的方法中。它接受一个数组或对象作为参数,数组中的每个元素都是一个字符串,表示 store 中的 mutation 名称。对象中的每个属性名表示方法的名称,属性值表示 store 中的 mutation 名称。 示例: ```javascript import { mapMutations } from 'vuex' export default { methods: { ...mapMutations([ 'increment', // 将 `this.increment()` 映射为 `this.$store.commit('increment')` 'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.commit('incrementBy', amount)` ]) } } ``` #### 4. mapActions `mapActions` 函数用于将 store 中的 actions 映射到组件的方法中。它接受一个数组或对象作为参数,数组中的每个元素都是一个字符串,表示 store 中的 action 名称。对象中的每个属性名表示方法的名称,属性值表示 store 中的 action 名称。 示例: ```javascript import { mapActions } from 'vuex' export default { methods: { ...mapActions([ 'increment', // 将 `this.increment()` 映射为 `this.$store.dispatch('increment')` 'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.dispatch('incrementBy', amount)` ]) } } ``` 这些辅助函数可以帮助我们更方便地使用 Vuex 中的状态、getters、mutations 和 actions。在使用它们时,我们可以将代码写得更简洁、更易读、更易维护。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值