I am facing a challenge with a list of components that I would like to make editable and replicate the state to the parent component.
The list component is defined as:
Vue.component("shortcuts", {
props: {
shortcuts: Array
},
template: '...<shortcut-entry v-for="(shortcut, index) in shortcuts" :key="index" :shortcut="shortcut" @remove="remove(index)"></shortcut-entry>...'
})
It is used with a model like this:
<shortcuts :shortcuts.sync="shortcuts"></shortcuts>
Now each shortcut-entry
component will contain multiple values that I want to pass back to the main list of objects:
Vue.component("shortcut-entry", {
props: {
mod_ctrl: Boolean,
mod_alt: Boolean,
mod_shift: Boolean,
keypress: String,
action: String,
...
},
Each of these properties has its own checkbox or input field on the page, for example: <input ... v-model="action">
. While I can manually handle updating the parent component for each change, it seems like a lot of repetitive code.
Is there a way to automatically propagate any changes made to these props back to the parent component? (while avoiding the "Avoid mutating a prop directly" warning)
So far, moving all props into another level and binding them with .sync
seems to work, but I am exploring a more explicit solution that declares these options upfront.