我沿着VueJS指南進行操作,目前我正在查看https://vuejs.org/v2/guide/components.html#Customizing-Component-v-model。VueJS v2指南。在非價值財產上使用V模型
我試圖創建一個最簡單的例子,但一直未能使其工作。
什麼樣的變化,我需要做,使下面的兩個HTML語句是可以互換的VueJS指導建議?:
<my-checkbox :checked="chicken" @change="val => { chicken = val }" value="A Bird"></my-checkbox>
和
<my-checkbox v-model="chicken" value="Chicken"></my-checkbox>
這裏是我有這樣的代碼遠。
// 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://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>