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

首頁 > 語言 > JavaScript > 正文

淺談Vuex@2.3.0 中的 state 支持函數申明

2024-05-06 15:24:12
字體:
來源:轉載
供稿:網友

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)

假如你 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 卻需要每個單獨設置, 如果直接設置在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 = actionsexport const _mutations = mutationsexport const _getters = gettersexport default {  namespaced: true,  state() {    return { lists: { data: [], total: 0, current_page: 1 } }  },  actions,  mutations,  getters}            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 砀山县| 定结县| 建阳市| 大关县| 琼海市| 电白县| 小金县| 镇沅| 阿拉善盟| 崇文区| 淅川县| 五河县| 东光县| 凤城市| 清流县| 广河县| 静海县| 东乌珠穆沁旗| 泾源县| 延川县| 威远县| 始兴县| 横峰县| 文登市| 临漳县| 涟水县| 稻城县| 鄂尔多斯市| 印江| 靖远县| 云安县| 兴海县| 岱山县| 神农架林区| 红河县| 祁门县| 罗定市| 乡城县| 冀州市| 许昌市| 北宁市|