淺談Vuex@2.3.0 中的 state 支持函數(shù)申明
vuex 2.3.0 的發(fā)布說(shuō)明: 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è)模塊公共出來(lái), 在 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è)問(wèn)題將不存在
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,
}
}
})
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
對(duì)vue事件的延遲執(zhí)行實(shí)例講解
今天小編就為大家分享一篇對(duì)vue事件的延遲執(zhí)行實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
vue.js vue-router如何實(shí)現(xiàn)無(wú)效路由(404)的友好提示
眾所周知vue-router是Vue.js官方的路由插件,它和vue.js是深度集成的,適合用于構(gòu)建單頁(yè)面應(yīng)用,下面這篇文章主要給大家介紹了關(guān)于vue.js中vue-router如何實(shí)現(xiàn)無(wú)效路由(404)的友好提示的相關(guān)資料,需要的朋友可以參考下。2017-12-12
詳解vuex持久化插件解決瀏覽器刷新數(shù)據(jù)消失問(wèn)題
這篇文章主要介紹了詳解vuex持久化插件解決瀏覽器刷新數(shù)據(jù)消失問(wèn)題,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04
Vue中使用jsencrypt進(jìn)行RSA非對(duì)稱加密的操作方法
這篇文章主要介紹了Vue中使用jsencrypt進(jìn)行RSA非對(duì)稱加密,在這里需要注意要加密的數(shù)據(jù)必須是字符串,對(duì)Vue?RSA非對(duì)稱加密相關(guān)知識(shí)感興趣的朋友一起看看吧2022-04-04
vue3實(shí)現(xiàn)H5表單驗(yàn)證組件的示例
本文主要介紹了vue3實(shí)現(xiàn)H5表單驗(yàn)證組件的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
Vue export import 導(dǎo)入導(dǎo)出的多種方式與區(qū)別介紹
這篇文章主要介紹了Vue export import 導(dǎo)入導(dǎo)出的多種方式與區(qū)別介紹,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
vue-router跳轉(zhuǎn)時(shí)打開(kāi)新頁(yè)面的兩種方法
這篇文章主要給大家介紹了關(guān)于vue-router跳轉(zhuǎn)時(shí)打開(kāi)新頁(yè)面的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue-router具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Vue如何解決子組件data從props中無(wú)法動(dòng)態(tài)更新數(shù)據(jù)問(wèn)題
這篇文章主要介紹了Vue如何解決子組件data從props中無(wú)法動(dòng)態(tài)更新數(shù)據(jù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
安裝VUE-CLI一直失敗的排錯(cuò)過(guò)程及解決方案
這篇文章主要介紹了安裝VUE-CLI一直失敗的排錯(cuò)過(guò)程及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10

