Skip to content

Instantly share code, notes, and snippets.

@richardpdorr
Last active December 18, 2021 18:30
Show Gist options
  • Save richardpdorr/70b3edda45a3fbe848ef4078c6f7ad2a to your computer and use it in GitHub Desktop.
Save richardpdorr/70b3edda45a3fbe848ef4078c6f7ad2a to your computer and use it in GitHub Desktop.
Extending a base store for Vuex for multiple Vue applications / instances (with some examples).
export const changeIsMobile = ({ commit }, isMobile) => {
commit('setIsMobile', isMobile);
};
export const changeLoading = ({ commit }, loading) => {
commit('setLoading', loading);
};
export const addError = ({ commit }, e) => {
commit('updateAddError', e);
};
export const resetAllErrors = ({ commit }) => {
commit('resetErrors');
};
import * as actions from './base-store/actions';
import * as mutations from './base-store/mutations';
import * as getters from './base-store/getters';
export default {
state: {
loading: false,
isMobile: false,
errors: []
},
getters,
mutations,
actions
}
export const getIsMobile = (state) => {
return state.isMobile;
}
export const getLoading = (state) => {
return state.loading;
}
export const getErrors = (state) => {
return state.errors;
}
export const setLoading = (state, loading) => {
state.loading = loading;
}
export const setIsMobile = (state, isMobile) => {
state.isMobile = isMobile;
}
export const updateAddError = (state, error) => {
state.errors.push(error);
}
export const resetErrors = (state) => {
state.errors = [];
}
....
import BaseStore from './path/to/resolve/BaseStore'
export const store = new Vuex.Store(Object.assign(
{},
BaseStore,
{ modules: { exampleModule1, exampleModule2 } }
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment