這篇文章主要介紹了淺談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 in multiple#s) in the same store, or in multiple# stores) in multiple#s) in ##假如你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, } } })以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網! 相關建議:
VUEJS 2.0 子元件存取/呼叫父元件
以上是關於Vuex@2.3.0 中的 state 支援函數的申明的詳細內容。更多資訊請關注PHP中文網其他相關文章!