這篇文章主要介紹了Vuex@2.3.0中state如何支持函數(shù)申明,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

10年積累的網(wǎng)站建設(shè)、成都網(wǎng)站制作經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有伍家崗免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
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 對(duì)象被引用共享
在 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ù)申明”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
當(dāng)前標(biāo)題:Vuex@2.3.0中state如何支持函數(shù)申明
文章鏈接:http://www.chinadenli.net/article4/iieioe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、Google、全網(wǎng)營(yíng)銷推廣、商城網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、python
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)