国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

淺談Vuex@2.3.0 中的 state 支持函數(shù)申明

2019-11-19 14:52:49
字體:
供稿:網(wǎng)友

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 = actionsexport const _mutations = mutationsexport const _getters = gettersexport 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 卻需要每個單獨(dú)設(shè)置, 如果直接設(shè)置在lists里, 會導(dǎo)致 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 = actionsexport const _mutations = mutationsexport const _getters = gettersexport 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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 宣化县| 上栗县| 南投县| 忻城县| 山阴县| 保康县| 西林县| 镇原县| 绥江县| 盐津县| 高密市| 岳池县| 新兴县| 湘阴县| 磐安县| 镇沅| 云安县| 乐亭县| 呼玛县| 简阳市| 高淳县| 潮安县| 沙雅县| 白城市| 永定县| 广丰县| 隆回县| 南溪县| 榆树市| 富裕县| 凤台县| 广河县| 益阳市| 孟连| 特克斯县| 宜城市| 灌阳县| 克东县| 遵化市| 常德市| 康平县|