Vuex@2.3.0 中的 state 支持函数申明了

vuex 2.3.0 的发布说明: Modules can now declare state using a function - this allows the same module definition to be reused (e.g. multiple times in the same store, or in multiple stores)

假如你 vuex 的模块有多个格式是完全一样的, 这时候就可以把这个模块公共出来, 在 Vuex 实例里引用, 如:

import api from '~api'

const actions = {
    async ['get']({commit, rootState: {route: { path }}}, config = {}) {
        const { data: { code, data } } = await api.post(config.url, config.data)
        if (code === 1001) commit('receive', data)
    }
}

const mutations = {
    ['receive'](state, data) {
        state.data = [].concat(data)
    },
    ['modify'](state, payload) {
        const index = state.data.findIndex(item => item.id === payload.id)
        if (index > -1) {
            state.data.splice(index, 1, payload)
        }
    },
    ['insert'](state, payload) {
        state.data = [payload].concat(state.data)
    },
    ['remove'](state, id) {
        const index = state.data.findIndex(item => item.id === id)
        state.data.splice(index, 1)
    }
}

const getters = {
    ['get'](state) {
        return state.data
    }
}
export const _actions = actions
export const _mutations = mutations
export const _getters = getters
export default {
    namespaced: true,
    actions,
    mutations,
    getters
}
import Vue from 'vue'
import Vuex from 'vuex'

import lists from './general/lists'

Vue.use(Vuex)

export default new Vuex.Store({
    namespaced: true,
    modules: {
        base: {
            namespaced: true,
            modules: {
                app: {...lists, state: { lists: { data: [], total: 0, current_page: 1 } }},
                platform: {...lists, state: { lists: { data: [], total: 0, current_page: 1 } }},
                product: {
                    namespaced: true,
                    modules: {
                        category: {...lists, state: { lists: { data: [], total: 0, current_page: 1 } }},
                    }
                },
                keyword: {
                    namespaced: true,
                    modules: {
                        username: {...lists, state: { lists: { data: [], total: 0, current_page: 1 } }},
                    }
                },
            }
        },
        buzz: {
            namespaced: true,
            modules: {
                shop: {...lists, state: { lists: { data: [], total: 0, current_page: 1 } }},
            }
        }
})

但是 state 却需要每个单独设置, 如果直接设置在lists里, 会导致 state 对象被引用共享

vuex@2.3.0 中, 这个问题将不存在

import api from '~api'

const actions = {
    async ['get']({commit, rootState: {route: { path }}}, config = {}) {
        const { data: { code, data } } = await api.post(config.url, config.data)
        if (code === 1001) commit('receive', data)
    }
}

const mutations = {
    ['receive'](state, data) {
        state.data = [].concat(data)
    },
    ['modify'](state, payload) {
        const index = state.data.findIndex(item => item.id === payload.id)
        if (index > -1) {
            state.data.splice(index, 1, payload)
        }
    },
    ['insert'](state, payload) {
        state.data = [payload].concat(state.data)
    },
    ['remove'](state, id) {
        const index = state.data.findIndex(item => item.id === id)
        state.data.splice(index, 1)
    }
}

const getters = {
    ['get'](state) {
        return state.data
    }
}
export const _actions = actions
export const _mutations = mutations
export const _getters = getters
export default {
    namespaced: true,
    state() {
        return { lists: { data: [], total: 0, current_page: 1 } }
    },
    actions,
    mutations,
    getters
}
import Vue from 'vue'
import Vuex from 'vuex'

import lists from './general/lists'

Vue.use(Vuex)

export default new Vuex.Store({
    namespaced: true,
    modules: {
        base: {
            namespaced: true,
            modules: {
                app: lists,
                platform: lists,
                product: {
                    namespaced: true,
                    modules: {
                        category: lists,
                    }
                },
                keyword: {
                    namespaced: true,
                    modules: {
                        username: lists,
                    }
                },
            }
        },
        buzz: {
            namespaced: true,
            modules: {
                shop: lists,
            }
        }
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值