麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > JavaScript > 正文

淺談[email protected] 中的 state 支持函數申明

2019-11-19 14:52:49
字體:
來源:轉載
供稿:網友

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 對象被引用共享

[email protected] 中, 這個問題將不存在

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,      }    }})

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 午夜精品小视频 | 91成人在线网站 | 激情小说激情电影 | 欧美偷拍一区二区 | 国产精品一品二区三区四区18 | 中国的免费的视频 | 亚洲一区二区国产 | 宅男噜噜噜66国产在线观看 | 国产寡妇xxxxxxxx性开放 | 亚洲人成网在线观看 | 久久国产精品电影 | 久久激情小视频 | 美女羞羞视频在线观看 | 一级免费在线 | 日本xxxx色视频在线观看免费, | 蜜桃视频最新网址 | 国产va在线观看 | 天堂二区| 亚洲一区二区国产 | 一区二区免费看 | 日韩中文字幕一区二区三区 | 欧美精品一区二区三区久久久 | 日本中文字幕电影在线观看 | 国产国语毛片 | 日本在线不卡一区二区 | 欧美熟videos肥婆 | 操皮视频 | 久久国产精品一区 | 性欧美xxxx免费岛国不卡电影 | 久久久久久久免费看 | 久久经典视频 | 99影视在线视频免费观看 | 日韩高清影视 | 日本在线视频免费 | 国产精品久久在线观看 | 黄片毛片一级 | 成人精品久久 | 制服丝袜成人动漫 | 欧美a在线观看 | 精品一区二区亚洲 | 精品久久中文字幕 |