Answer the question
In order to leave comments, you need to log in
Is there a better way or how can this code be improved?
I had the following task: to send changes from input (range) to different ключ:значение
ones in the Vuex storage,
but the fact is that everything was distributed among objects there, and there was not one input.
The task was such that the values from the sliders had to fill their own ключ:значения
in the Vuex store object.
Imagine we have a storage state like this:
state: {
CarSetup: {
wheels: 0,
color: 0,
sim: 0,
},
}
data(){
return{
rangeValues: {
wheels: 0,
color: 0,
sim: 0
},
}
},
computed: {
returnValue() {
return { ...this.rangeValues };
},
},
watch:{
returnValue(newValue, oldValue) {
if(newValue.wheels!= oldValue.wheels){
this.$store.commit('setCarInfo', {to: 'wheels', value: newValue.wheels})
}
if(newValue.color!= oldValue.color){
this.$store.commit('setCarInfo', {to: 'color', value: newValue.color})
}
if(newValue.sim!= oldValue.sim){
this.$store.commit('setCarInfo', {to: 'sim', value: newValue.sim})
}
},
}
mutations: {
setCarInfo(store, v){
store.CarSetup[v.to] = v.value
},
},
Answer the question
In order to leave comments, you need to log in
Cut out returnValue, make the rangeValues property from the usual one calculated:
computed: {
rangeValues() {
return new Proxy(this.$store.state.carSetup, {
set: (target, key, val) => {
this.$store.commit('setCarInfo', { [key]: val });
return true;
},
});
},
},
setCarInfo: (state, payload) => Object.assign(state.carSetup, payload),
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question