最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

关于Vuex@2.3.0中的state支持函数的申明

来源:动视网 责编:小采 时间:2020-11-27 19:34:48
文档

关于Vuex@2.3.0中的state支持函数的申明

关于Vuex@2.3.0中的state支持函数的申明:这篇文章主要介绍了浅谈Vuex@2.3.0 中的 state 支持函数申明,内容挺不错的,现在分享给大家,也给大家做个参考。vuex 2.3.0 的发布说明: Modules can now declare state using a function - this allows the same modul
推荐度:
导读关于Vuex@2.3.0中的state支持函数的申明:这篇文章主要介绍了浅谈Vuex@2.3.0 中的 state 支持函数申明,内容挺不错的,现在分享给大家,也给大家做个参考。vuex 2.3.0 的发布说明: Modules can now declare state using a function - this allows the same modul


这篇文章主要介绍了浅谈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)

假如你 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,
 }
 }
})

文档

关于Vuex@2.3.0中的state支持函数的申明

关于Vuex@2.3.0中的state支持函数的申明:这篇文章主要介绍了浅谈Vuex@2.3.0 中的 state 支持函数申明,内容挺不错的,现在分享给大家,也给大家做个参考。vuex 2.3.0 的发布说明: Modules can now declare state using a function - this allows the same modul
推荐度:
标签: VUE 函数 state
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top