I have a custom radio button component with the following HTML markup:
<div id="app">
{{message}} - {{sex}}
<radio value="m" v-model="sex" name="sex">Male</radio>
<radio value="f" v-model="sex" name="sex">Female</radio>
</div>
The component (and app) are defined as follows:
Vue.component('radio', {
model : {
prop:'checked',
event:'change'
},
props : {
value:null,
name:String
},
data : () => ({
val:''
}),
methods : {
update (e) {
this.$emit('change',e.target.value);
}
},
template:'<div><input :value="value" :name="name" type="radio" v-model="val" @change="update"><slot></slot></div>'
})
var app = new Vue({
el: '#app',
data: {
message: 'selected:',
sex:''
}
})
It switches the radio buttons correctly (for the most part).
Problem: in order to do so I have to click Male radio button twice. I'd like to be able to only click it once to toggle them. Perhaps I'm not using the model
attribute described here correctly?
I saw this question, but it uses older Vue (which doesn't have model
attribute) and I don't want to have to capture @change on the parent (the entire idea behind the separate component is to abstract as much logic as possible)
update: For anyone interested here is the solution thanks to @thanksd