I am using vuejs two way binding to change a property in another component, by emiting an event.
This is how I send the event from cmponent 1:
<input
v-model="form.title"
@keyup="$root.$emit('updateItemEvent', {
key: 'section.title',
value: form.title
})"
type="text"
>
I want to use the same event in the componsnt many times with different "key" and "value".
Here is component 2 where I listen to the event
NOTE: section object could be in data or props
data() {
return {
section: {title:'old title', ...},
},
},
mounted() {
this.$root.$on('updateItemEvent', (data) => {
// data variable structure is: {key:'section.title', value:'new modified title'}
// data.key // is a key in this component data
// data.value // the new value I want to assign to the data.key in this component
//this is the problem, I want to access a property dynamically by name.
//eg. this.section.title should be accessible like this "this.{data.key}"
this.data.key = data.value// ---> this is the error, how to do something like this?
})
},
how to access a property by name dynamically like this.data.key