您好,登錄后才能下訂單哦!
這篇文章主要介紹了Vuex@2.3.0中state如何支持函數(shù)申明,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
vuex 2.3.0 的發(fā)布說明: 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 的模塊有多個(gè)格式是完全一樣的, 這時(shí)候就可以把這個(gè)模塊公共出來, 在 Vuex 實(shí)例里引用, 如:
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 卻需要每個(gè)單獨(dú)設(shè)置, 如果直接設(shè)置在lists里, 會(huì)導(dǎo)致 state 對象被引用共享
在 vuex@2.3.0 中, 這個(gè)問題將不存在
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, } } })
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Vuex@2.3.0中state如何支持函數(shù)申明”這篇文章對大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。