Skip to content

Instantly share code, notes, and snippets.

@huybuidac
Last active February 2, 2020 08:02
Show Gist options
  • Select an option

  • Save huybuidac/e2cbe023f52bbdab2071e90624588a5d to your computer and use it in GitHub Desktop.

Select an option

Save huybuidac/e2cbe023f52bbdab2071e90624588a5d to your computer and use it in GitHub Desktop.
Vuex Mixins
import Vue from 'vue'
import Vuex from 'vuex'
import { createStore } from 'vuex-extensions'
Vue.use(Vuex)
const store = createStore(Vuex.Store, {
modules: {
moduleA: {
namespaced: true,
state: {
staffs: [],
departments: []
},
actions: {
fetch ({commit}) {
const staffs = []
const departments = []
commit('changeState', { staffs, departments }) // commit from module
}
}
}
},
mixins: {
mutations: {
changeState: function (state, changed) {
Object.entries(changed)
.forEach(([name, value]) => {
state[name] = value
})
}
}
}
})
const staffs = []
const departmentList = []
// commit from global
store.commit('moduleA/changeState', {
staffs,
departments: departmentList
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment