Answer the question
In order to leave comments, you need to log in
VueJs, Vuex creating default state for modules?
Good afternoon, I am using laravel + vuex with the structure in the picture above. Not long ago, I began to study this whole thing. Everything works as it should, and now the question arose about the state of all modules, because it is not reset when routing, and I like to call the necessary components in another component so as not to run through the routes, and in the future it will be necessary. If successful, I immediately reset the state, but if there is an error, then it must be displayed (for example, a car with such a state number already exists) and the error remains in the state, errors must always be cleared through a mutation call ... Therefore, I thought, why not with routing, do not call defaultstate, so that information about errors in the state, for example, is cleared. But knowledge is not enough. And where is it better to call it, I think in routes.js during transitions, call defaultstate all the time, but it seems this is not a good idea,
For example, my cars module:
/cars/index :
import state from './state';
import * as getters from './getters';
import * as mutations from './mutations';
import * as actions from './actions';
export default {
namespaced: true,
state,
getters,
mutations,
actions
}
/cars/state :
export default {
cars: [],
car: {},
snackbar: {
timeout:4000,
color:null,
show: false,
text: null,
},
form: {
name: null,
vin: null,
mark: null,
model: null,
fuel: null,
type: null,
tarif: null,
department_id: null
},
errors: {}
}
/store/index
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use( Vuex );
import state from './state.js';
import * as getters from './getters.js';
import * as mutations from './mutations.js';
import * as actions from './actions.js';
import upl from './modules/uploads/';
import dep from './modules/departments/';
import car from './modules/cars/';
export default new Vuex.Store({
namespaced: true,
state,
mutations,
actions,
modules: {
dep, upl, car
}
});
In actions and mutations, I think, there is nothing interesting, because. there are no problems.
Answer the question
In order to leave comments, you need to log in
Just declare a function that will return the default state for the module. And make a mutation in which the default state will be set by calling this function. But when to call this mutation: in component hooks or in router hooks or sometime else will already depend on your needs and application logic.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question