SoFunction
Updated on 2025-04-05

How to understand the use of Vue's .sync modifier

This article introduces the use of Vue's .sync modifier, share it with everyone, and leave a note for yourself

Case

<div >
  <div>{{bar}}</div>
  <my-comp :="bar"></my-comp>
  <!-- <my-comp :foo="bar" @update:foo="val => bar = val"></my-comp> -->
</div>
<script>
  ('my-comp', {
    template: '<div @click="increment">Click me+1</div>',
    data: function() {
      return {copyFoo: }
    },
    props: ['foo'],
    methods: {
      increment: function() {
        this.$emit('update:foo', ++);
      }
    }
  });
  new Vue({
    el: '#app',
    data: {bar: 0}
  });
</script>

Note: The code <my-comp :="bar"></my-comp> will be expanded to <comp :foo="bar" @update:foo="val => bar = val"></comp>, which is a syntactic sugar.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.