案例app
<div id="app"> <div>{{bar}}</div> <my-comp :foo.sync="bar"></my-comp> <!-- <my-comp :foo="bar" @update:foo="val => bar = val"></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>,就是一個語法糖。this