大型Vuex项目 ,使用module后, 如何调用其他模块的 属性值和方法?

https://vuex.vuejs.org/zh-cn/modules.html

https://www.cnblogs.com/yeziTesting/p/7182904.html


Vuex 允许我们把 store 分 module(模块)。每一个模块包含各自的状态、mutation、action 和 getter。

那么问题来了, 模块化+命名空间之后, 数据都是相对独立的, 如果想在模块 A 调用 模块 B 的state, actions, mutations, getters, 该肿么办?

假设有这么两个模块:

模块A:
import api from '~api'

const state = {
    vip: {},
}

const actions = {
    async ['get']({commit, state, dispatch}, config = {}) {
        try {
            const { data: { code, data } } = await api.post('vip/getVipBaseInfo', config)
            if (code === 1001) commit('receive', data)
        } catch(error) { console.log(error) }
    }
}

const mutations = {
    ['receive'](state, data) {
        state.vip = data
    }
}

const getters = {
    ['get'](state) {
        return state.vip
    },
}

export default {
    namespaced: true,
    state,
    actions,
    mutations,
    getters
}
模块B:
import api from '~api'

const state = {
    shop: {},
}

const actions = {
    async ['get']({commit, state, dispatch}, config = {}) {
        try {
            const { data: { code, data } } = await api.post('shop/getShopBaseInfo', config)
            if (code === 1001) commit('receive', data)
        } catch(error) { console.log(error) }
    }
}

const mutations = {
    ['receive'](state, data) {
        state.shop = data
    }
}

const getters = {
    ['get'](state) {
        return state.shop
    },
}

export default {
    namespaced: true,
    state,
    actions,
    mutations,
    getters
}
假设模块 B 的 actions 里, 需要用模块 A 的 state 该怎么办?
const actions = {
    async ['shop'](store, config = {}) {
        const { commit, dispatch, state, rootState } = store
        console.log(rootState) // 打印根 state
        console.log(rootState.vip) // 打印其他模块的 state
        try {
            const { data: { code, data } } = await api.post('shop/getShopBaseInfo', config)
            if (code === 1001) commit('receive', data)
        } catch(error) { console.log(error) }
    }
}

我们来看下上面的代码, actions 中的 shop 方法, 有 2 个参数, 第一个是 store, 第二个是 dispatch 调用时传过来的参数
store 这个对象又包含了 4 个键, 其中 commit 是调用 mutations 用的, dispatch 是调用 actions 用的, state 是当前模块的 state, 而 rootState 是根 state,
既然能拿到根 state, 想取其他模块的 state 是不是就很简单了...?

假设模块 B 的 actions 里, 需要调用模块 A 的 actions 该怎么办?
const actions = {
    async ['shop'](store, config = {}) {
        const { commit, dispatch, state, rootState } = store
        try {
            const { data: { code, data } } = await api.post('shop/getShopBaseInfo', config, 'get')
            if (code === 1001) commit('receive', data) // 调用当前模块的 mutations
            dispatch('vip/get', {}, {root: true}) // 调用其他模块的 actions
        } catch(error) { console.log(error) }
    }
}

上面的代码中dispatch('vip/vip', {}, {root: true})就是在模块 B 调用 模块 A 的 actions,
有 3 个参数, 第一个参数是其他模块的 actions 路径, 第二个是传给 actions 的数据, 如果不需要传数据, 也必须预留, 第三个参数是配置选项, 申明这个 acitons 不是当前模块的

假设模块 B 的 actions 里, 需要调用模块 A 的 mutations 该怎么办?
const actions = {
    async ['shop'](store, config = {}) {
        const { commit, dispatch, state, rootState } = store
        try {
            const { data: { code, data } } = await api.post('shop/getShopBaseInfo', config)
            if (code === 1001) commit('receive', data) // 调用当前模块的 mutations
            commit('vip/receive', data, {root: true}) // 调用其他模块的 mutations
        } catch(error) { console.log(error) }
    }
}

上面的代码中commit('vip/receive', {}, {root: true})就是在模块 B 调用 模块 A 的 mutations,
有 3 个参数, 第一个参数是其他模块的 mutations 路径, 第二个是传给 mutations 的数据, 如果不需要传数据, 也必须预留, 第三个参数是配置选项, 申明这个 mutations 不是当前模块的

假设模块 B 的 actions 里, 需要用模块 A 的 getters 该怎么办?
const actions = {
    async ['shop'](store, config = {}) {
        const { commit, dispatch, state, rootState, rootGetters } = store
        console.log(rootGetters['vip/get']) // 打印其他模块的 getters
        try {
            const { data: { code, data } } = await api.post('shop/getShopBaseInfo', config)
            if (code === 1001) commit('receive', data)
        } catch(error) { console.log(error) }
    }
}

我们来看下上面的代码, 相比之前的代码, store 又多了一个键: rootGetters
rootGetters 就是 vuex 中所有的 getters, 你可以用 rootGetters['xxxxx'] 来取其他模块的getters


Vuex一个专门为 Vue.js 应用程序开发的状态管理库,用于管理组件之间共享的状态。Vuex 有五个核心属性:state、mutations、actions、getters 和 modules。 1. state:state 是一个存储数据的对象,用于存储应用程序中的状态。可以通过 this.$store.state.xxx 访问该属性。 2. mutations:mutations 是用于修改 state 中的数据的方法,它们必须是同步函数。可以通过 this.$store.commit('mutationName') 调用 mutations 中的方法。 3. actions:actions 用于处理异步操作和异步任务,可以通过 this.$store.dispatch('actionName') 调用 actions 中的方法。actions 中可以提交 mutations 来修改 state 中的数据。 4. getters:getters 用于访问 state 中的数据并进行计算,类似于 Vue.js 中的计算属性。可以通过 this.$store.getters.getterName 访问 getters 中的方法。 5. modules:modules 允许将 store 分割成模块,每个模块拥有自己的 state、mutations、actions 和 getters。可以通过 this.$store.state.moduleName.xxx 访问模块中的 state 属性使用方法: 1. 安装 Vuex:npm install vuex --save 2. 创建 store.js 文件,定义 state、mutations、actions、getters 和 modules 等属性。 3. 在 Vue 组件中引入 Vuex 并注册 store。 4. 在 Vue 组件中通过 this.$store.state.xxx、this.$store.commit('mutationName')、this.$store.dispatch('actionName') 和 this.$store.getters.getterName 访问和修改状态。 示例代码: store.js 文件: ``` import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const store = new Vuex.Store({ state: { count: 0 }, mutations: { increment (state) { state.count++ } }, actions: { increment (context) { context.commit('increment') } }, getters: { getCount: state => { return state.count } }, modules: { moduleA: { state: {}, mutations: {}, actions: {}, getters: {} }, moduleB: { state: {}, mutations: {}, actions: {}, getters: {} } } }) export default store ``` App.vue 文件: ``` <template> <div> <p>Count: {{ count }}</p> <button @click="increment">Increment</button> <p>GetCount: {{ getCount }}</p> </div> </template> <script> export default { computed: { count () { return this.$store.state.count }, getCount () { return this.$store.getters.getCount } }, methods: { increment () { this.$store.commit('increment') this.$store.dispatch('increment') } } } </script> ``` 以上是一个简单的 Vuex 示例,可以通过按钮点击来增加 count 的,并通过 getters 获取 count 的
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天崩地裂金刚不坏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值