I am following along the VueJS guide, and I am looking at https://v2.vuejs.org/v2/guide/components.html#Customizing-Component-v-model at the moment.
I tried to create a minimal example, but haven't been able to make it work.
What changes do I need to make so that the following two HTML statements are interchangeable as the VueJS guide suggests?:
<my-checkbox :checked="chicken" @change="val => { chicken = val }" value="A Bird"></my-checkbox>
and
<my-checkbox v-model="chicken" value="Chicken"></my-checkbox>
Here's the code I've got so far.
// Components#CustomizingComponentVModel
Vue.component('myCheckbox', {
model: { prop: 'checked', emit: 'change' },
props: ['value'],
methods: {
uVal: function(checkStatus) {
this.$emit('change', checkStatus);
}
},
template: '<span>' +
'<input type="checkbox" @change="uVal($event.target.checked)">' +
'<label>{{ value }}</label>' +
'</span>'
});
// Default app
new Vue({
el: '#app',
data: function() {
return { chicken: false };
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.3/vue.js"></script>
<!--
I am trying to create a minimal example of customizing v-model described at
https://v2.vuejs.org/v2/guide/components.html#Customizing-Component-v-model
-->
<div id="app">
<my-checkbox :checked="chicken" @change="val => { chicken = val }" value="A Bird"></my-checkbox>
<!-- VueJS guide suggest that previous line is same as the next one: -->
<!-- <my-checkbox v-model="chicken" value="Chicken"></my-checkbox> -->
<!-- If I comment line 9 and uncomment line 11, what changes do I need to make in my code to make this work? Also, why are those changes needed? -->
<p>Chicken: {{ chicken }}</p>
</div>