当前位置:首页 > VUE

vue实现双向

2026-02-10 09:01:44VUE

Vue 实现双向绑定的方法

Vue 的双向绑定主要通过 v-model 指令实现,它结合了 v-bind(属性绑定)和 v-on(事件监听)的功能。以下是几种常见的实现方式:

使用 v-model 指令

v-model 是 Vue 提供的语法糖,可以简化表单元素的双向绑定。

vue实现双向

<template>
  <input v-model="message" placeholder="输入内容">
  <p>输入的内容是:{{ message }}</p>
</template>

<script>
export default {
  data() {
    return {
      message: ''
    }
  }
}
</script>

自定义组件实现双向绑定

在自定义组件中,可以通过 model 选项和 $emit 实现双向绑定。

<template>
  <custom-input v-model="inputValue"></custom-input>
  <p>绑定的值:{{ inputValue }}</p>
</template>

<script>
export default {
  data() {
    return {
      inputValue: ''
    }
  },
  components: {
    'custom-input': {
      props: ['value'],
      model: {
        prop: 'value',
        event: 'input'
      },
      template: `
        <input 
          :value="value" 
          @input="$emit('input', $event.target.value)"
        >
      `
    }
  }
}
</script>

使用 .sync 修饰符

Vue 2.x 中可以通过 .sync 修饰符实现父子组件的双向绑定。

vue实现双向

<template>
  <custom-input :value.sync="syncValue"></custom-input>
  <p>同步的值:{{ syncValue }}</p>
</template>

<script>
export default {
  data() {
    return {
      syncValue: ''
    }
  },
  components: {
    'custom-input': {
      props: ['value'],
      template: `
        <input 
          :value="value" 
          @input="$emit('update:value', $event.target.value)"
        >
      `
    }
  }
}
</script>

Vue 3 中的 v-model 改进

Vue 3 对 v-model 进行了优化,支持多个 v-model 绑定。

<template>
  <custom-input v-model:firstName="first" v-model:lastName="last"></custom-input>
  <p>全名:{{ first }} {{ last }}</p>
</template>

<script>
export default {
  data() {
    return {
      first: '',
      last: ''
    }
  },
  components: {
    'custom-input': {
      props: {
        firstName: String,
        lastName: String
      },
      emits: ['update:firstName', 'update:lastName'],
      template: `
        <div>
          <input 
            :value="firstName" 
            @input="$emit('update:firstName', $event.target.value)"
          >
          <input 
            :value="lastName" 
            @input="$emit('update:lastName', $event.target.value)"
          >
        </div>
      `
    }
  }
}
</script>

双向绑定的底层原理

Vue 的双向绑定基于数据劫持和发布-订阅模式:

  1. 数据劫持:通过 Object.defineProperty(Vue 2)或 Proxy(Vue 3)监听数据变化。
  2. 依赖收集:在 getter 中收集依赖,在 setter 中通知更新。
  3. 模板编译:将模板解析为渲染函数,建立数据与视图的关联。
  4. 更新视图:数据变化时触发重新渲染。
// 简化的数据劫持示例(Vue 2)
function defineReactive(obj, key, val) {
  Object.defineProperty(obj, key, {
    get() {
      console.log(`读取 ${key}`);
      return val;
    },
    set(newVal) {
      console.log(`设置 ${key} 为 ${newVal}`);
      val = newVal;
    }
  });
}

以上方法覆盖了 Vue 中实现双向绑定的主要场景和技术细节。

标签: 双向vue
分享给朋友:

相关文章

vue实现div

vue实现div

Vue 实现 DIV 元素的方法 在 Vue 中实现 DIV 元素可以通过多种方式,以下是几种常见的实现方法: 使用模板语法 在 Vue 的模板中直接使用 HTML 的 <div> 标…

vue实现滑块

vue实现滑块

Vue 实现滑块组件的方法 使用原生 HTML 和 Vue 指令 通过 Vue 的 v-model 和事件监听实现基础滑块功能。创建一个包含 input 元素的组件,类型设置为 range,并绑定到…

vue compile 实现

vue compile 实现

Vue 编译实现原理 Vue 的编译过程将模板字符串转换为渲染函数,主要分为解析、优化和代码生成三个阶段。 解析阶段(Parse) 将模板字符串转换为抽象语法树(AST)。Vue 使用正则表达式和有…

vue实现frame

vue实现frame

Vue 中实现 iframe 的方法 在 Vue 中可以通过直接使用 <iframe> 标签或动态绑定 src 属性来实现 iframe 功能。 基本用法 <template…

vue实现长按

vue实现长按

Vue 实现长按功能的方法 在 Vue 中实现长按功能可以通过原生事件监听或自定义指令完成。以下是几种常见实现方式: 使用原生事件监听 通过 @mousedown 和 @mouseup 或 @t…

实现vue table

实现vue table

Vue 表格实现方法 基础表格实现 使用 Vue 的 v-for 指令可以快速渲染表格数据。在模板中定义表格结构,通过数据绑定动态生成行和列。 <template> <tabl…