Vue-常用代码片段


1、v-model & .Sync 如何实现

props: {
    value: {
      type: String,
      default: ''
    }
  },
  computed: {
    _value: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    }
  },

.Sync

props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },

v-model & .sync修饰符都可以实现数据的双向绑定

vue