浅谈Vuex@2.3.0 中的 state 支持函数申明

2020-06-16 06:14:30易采站长站整理

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