<div id="app">
<div>{{bar}}</div>
<my-comp :foo.sync="bar"></my-comp>
</div>
<script>
Vue.component('my-comp', {
template: '<div @click="increment">點我+1</div>',
data: function() {
return {copyFoo: this.foo}
},
props: ['foo'],
methods: {
increment: function() {
this.$emit('update:foo', ++this.copyFoo);
}
}
});
new Vue({
el: '#app',
data: {bar: 0}
});
</script>
- 說明:代碼<my-comp :foo.sync="bar"></my-comp>會被擴展成<comp :foo="bar" @update:foo="val => bar = val"></comp>,就是子組件觸發(fā)反饋值給父組件的一個語法糖拧揽。