Last active
February 2, 2020 08:02
-
-
Save huybuidac/e2cbe023f52bbdab2071e90624588a5d to your computer and use it in GitHub Desktop.
Vuex Mixins
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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