当前位置:首页 > VUE

vue实现双向绑定

2026-01-06 23:36:55VUE

Vue 双向绑定实现原理

Vue 的双向绑定是通过数据劫持结合发布者-订阅者模式实现的,核心是 Object.defineProperty(Vue 2.x)或 Proxy(Vue 3.x)。

Vue 2.x 实现方式

数据劫持 通过 Object.defineProperty 劫持对象的属性访问和修改:

Object.defineProperty(obj, key, {
  get() {
    return value;
  },
  set(newVal) {
    if (newVal !== value) {
      value = newVal;
      dep.notify(); // 通知订阅者更新
    }
  }
});

依赖收集 每个被劫持的属性都有一个 Dep 实例,用于管理所有订阅者(Watcher):

class Dep {
  constructor() {
    this.subs = [];
  }
  addSub(sub) {
    this.subs.push(sub);
  }
  notify() {
    this.subs.forEach(sub => sub.update());
  }
}

模板编译 Vue 编译器将模板解析为渲染函数,在解析过程中遇到指令(如 v-model)时会创建对应的 Watcher:

vue实现双向绑定

class Watcher {
  constructor(vm, expOrFn, cb) {
    this.vm = vm;
    this.getter = parsePath(expOrFn);
    this.cb = cb;
    this.value = this.get();
  }
  get() {
    Dep.target = this;
    const value = this.getter.call(this.vm, this.vm);
    Dep.target = null;
    return value;
  }
  update() {
    this.run();
  }
  run() {
    const value = this.get();
    if (value !== this.value) {
      const oldValue = this.value;
      this.value = value;
      this.cb.call(this.vm, value, oldValue);
    }
  }
}

Vue 3.x 实现方式

Vue 3 改用 Proxy 实现数据劫持,解决了 Vue 2 中无法检测数组和对象新增属性的限制:

const handler = {
  get(target, key) {
    track(target, key); // 依赖收集
    return Reflect.get(target, key);
  },
  set(target, key, value) {
    const result = Reflect.set(target, key, value);
    trigger(target, key); // 触发更新
    return result;
  }
};
const observed = new Proxy(raw, handler);

实现双向绑定的示例

Vue 2.x 风格实现

vue实现双向绑定

function defineReactive(obj, key, val) {
  const dep = new Dep();
  Object.defineProperty(obj, key, {
    get() {
      if (Dep.target) dep.addSub(Dep.target);
      return val;
    },
    set(newVal) {
      if (newVal === val) return;
      val = newVal;
      dep.notify();
    }
  });
}

Vue 3.x 风格实现

function reactive(obj) {
  return new Proxy(obj, {
    get(target, key) {
      track(target, key);
      return Reflect.get(target, key);
    },
    set(target, key, value) {
      const result = Reflect.set(target, key, value);
      trigger(target, key);
      return result;
    }
  });
}

在组件中使用双向绑定

通过 v-model 指令实现表单元素的双向绑定:

<input v-model="message">
<!-- 等价于 -->
<input :value="message" @input="message = $event.target.value">

对于自定义组件,需要通过 model 选项配置:

Vue.component('custom-input', {
  props: ['value'],
  model: {
    prop: 'value',
    event: 'input'
  },
  template: `
    <input 
      :value="value"
      @input="$emit('input', $event.target.value)"
    >
  `
});

标签: 绑定双向
分享给朋友:

相关文章

vue怎么实现双向

vue怎么实现双向

Vue 双向绑定的实现方式 Vue 中实现双向绑定的核心机制是 v-model 指令,它结合了属性绑定和事件监听,适用于表单元素或自定义组件。 表单元素的双向绑定 在原生表单元素(如 input、s…

实现vue数组双向绑定

实现vue数组双向绑定

Vue 数组双向绑定的实现方法 Vue 通过数据劫持和发布-订阅模式实现数据的双向绑定,但直接修改数组的某些方法(如索引赋值)无法触发视图更新。以下是实现数组双向绑定的几种方法: 使用 Vue.se…

vue 双向绑定 实现原理

vue 双向绑定 实现原理

Vue 双向绑定的实现原理 Vue 的双向绑定是通过数据劫持结合发布者-订阅者模式实现的,核心是 Object.defineProperty(Vue 2.x)或 Proxy(Vue 3.x)。以下是具…

vue中实现双向数据

vue中实现双向数据

Vue 中实现双向数据绑定的方法 使用 v-model 指令 v-model 是 Vue 提供的语法糖,用于在表单元素上实现双向数据绑定。它会根据控件类型自动选取正确的方法来更新元素的值。 <…

vue自己实现双向绑定

vue自己实现双向绑定

实现 Vue 双向绑定的核心原理 双向绑定的核心是通过数据劫持结合发布-订阅模式实现的。主要涉及 Object.defineProperty 或 Proxy 来监听数据变化,并通过观察者模式通知视图更…

vue数据双向绑定实现

vue数据双向绑定实现

Vue 数据双向绑定的实现原理 Vue 的双向数据绑定主要通过 数据劫持 和 发布-订阅模式 实现,核心是 Object.defineProperty(Vue 2.x)或 Proxy(Vue 3.x)…