淺談Vuex@2.3.0 中的 state 支持函數(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 的模塊有多個格式是完全一樣的, 這時候就可以把這個模塊公共出來, 在 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,
}
}
})
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue.js vue-router如何實現(xiàn)無效路由(404)的友好提示
眾所周知vue-router是Vue.js官方的路由插件,它和vue.js是深度集成的,適合用于構(gòu)建單頁面應用,下面這篇文章主要給大家介紹了關(guān)于vue.js中vue-router如何實現(xiàn)無效路由(404)的友好提示的相關(guān)資料,需要的朋友可以參考下。2017-12-12
詳解vuex持久化插件解決瀏覽器刷新數(shù)據(jù)消失問題
這篇文章主要介紹了詳解vuex持久化插件解決瀏覽器刷新數(shù)據(jù)消失問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04
Vue中使用jsencrypt進行RSA非對稱加密的操作方法
這篇文章主要介紹了Vue中使用jsencrypt進行RSA非對稱加密,在這里需要注意要加密的數(shù)據(jù)必須是字符串,對Vue?RSA非對稱加密相關(guān)知識感興趣的朋友一起看看吧2022-04-04
Vue export import 導入導出的多種方式與區(qū)別介紹
這篇文章主要介紹了Vue export import 導入導出的多種方式與區(qū)別介紹,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02
vue-router跳轉(zhuǎn)時打開新頁面的兩種方法
這篇文章主要給大家介紹了關(guān)于vue-router跳轉(zhuǎn)時打開新頁面的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用vue-router具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-07-07
Vue如何解決子組件data從props中無法動態(tài)更新數(shù)據(jù)問題
這篇文章主要介紹了Vue如何解決子組件data從props中無法動態(tài)更新數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10

