話很少說,直接上代碼javascript
<div id="app">java
<price-input v-bind:value="price" v-on:input="price = arguments[0]" ></price-input>app
<price-input v-model='price' ></price-input>this
<p>{{price}}</p>spa
</div>code
<script type="text/javascript ">component
Vue.component('price-input', {blog
template: '<input :value="value " @input="updateVal" type="text ">',ip
props: ["value "],get
methods: {
updateVal: function() {
let val = event.target.value
this.$emit('input',val);
}
}
});
var app = new Vue({
el: '#app',
data: {
price: ''
},
});
</script>
大體流程爲子組件的輸入值改變,觸發oninput方法,進而經過emit觸發實例上的input方法,進而給父組件中的狀態賦值。