Answer the question
In order to leave comments, you need to log in
How to use in v-model the properties of an object that is the value of a computed property?
there is an object in store.js
user: {
firstName: '',
lastName: '',
age: '',
}
<template>
form
input(type='text', placeholder='firstName')
input(type='text', placeholder='lastName')
input(type='text', placeholder='age')
</template>
<template>
form
input(type='text', placeholder='firstName', v-model='user.firstName')
input(type='text', placeholder='lastName', v-model='user.lastName')
input(type='text', placeholder='age', v-model='user.age')
</template>
<script>
export default {
computed: {
user: {
get() {
return this.$store.state.user;
},
set(value) {
this.$store.commit('saveUser', value);
},
}
},
};
</script>
Answer the question
In order to leave comments, you need to log in
The computed property will be an object from the state, wrapped in a Proxy to intercept the setting of property values and cause a mutation:
user() {
return new Proxy(this.$store.state.user, {
set: (target, prop, value) => {
this.$store.commit('updateUser', { [prop]: value });
return true;
},
});
},
updateUser(state, payload) {
Object.assign(state.user, payload);
},
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question