本篇文章主要介绍了如何理解Vue的.sync修饰符的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
<div id="app">
<div>{{bar}}</div>
<my-comp :foo.sync="bar"></my-comp>`<br />
<!-- <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>
就是一个语法糖。复制代码
转载于:https://juejin.im/post/5ad7f2696fb9a045cd030503