This is how I am currently handling it:
Parent.vue:
// Template
<form-child :schema="schema"><form-child>
// JS
data () {
return {
schema: [{ // name: '', value: '', type: '' }, { //etc ... }]
}
}
FormChild.vue:
// Template
<div v-for="field in schema">
<input v-if="field.type === 'text'" @change="updateValue(field.name, field.value)">
<textarea v-if="field.type === 'textarea'" @change="updateValue(field.name, field.value)">/textarea>
</div>
// JS
props: {
schema: Arrary
}
methods: {
updateValue (fieldName, fieldValue) {
this.schema.forEach(field => {
// this will update the schema in Parent.vue
if (field.name === fieldName) field.value = fieldValue
})
}
}
Do you think there might be a more effective way to handle this, maybe using emit
and v-model
? If so, could you please show me an example of how to implement that?