zoukankan      html  css  js  c++  java
  • 【转】大型Vuex项目 ,使用module后, 如何调用其他模块的 属性值和方法

    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) }
        }
    }
    

    上面的代码中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

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

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

    转自:https://blog.csdn.net/baidu_31333625/article/details/80351638

  • 相关阅读:
    初学者--bootstrap(六)组件中的字体图标----在路上(9)
    clearfix的最佳方案----在路上(22)
    css的五种属性值----在路上(21)
    float---浮动带来的影响与清除浮动带来的影响方法----在路上(20)
    form表单 ----在路上(15)
    css中常见的属性-----在路上(14)
    Linux | 管道、重定向命令
    C++ | from_string函数的写法
    C++/C | 关于char* char[] char = new char[n]
    C++内存管理 | 01 C++ memory primitives
  • 原文地址:https://www.cnblogs.com/hycms/p/9291950.html
Copyright © 2011-2022 走看看