当前位置:首页 > VUE

实现vue双向绑定

2026-02-11 01:13:00VUE

Vue 双向绑定的实现原理

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

基于 Object.defineProperty 的实现(Vue 2.x)

数据劫持 通过 Object.defineProperty 劫持对象的属性,在属性被访问或修改时触发 getter 和 setter:

function defineReactive(obj, key, val) {
  Object.defineProperty(obj, key, {
    get() {
      console.log(`获取 ${key}: ${val}`);
      return val;
    },
    set(newVal) {
      if (newVal !== val) {
        console.log(`设置 ${key}: ${newVal}`);
        val = newVal;
      }
    }
  });
}

依赖收集 每个被劫持的属性会维护一个订阅者列表(Dep),在 getter 中收集依赖,在 setter 中通知更新:

实现vue双向绑定

class Dep {
  constructor() {
    this.subscribers = [];
  }
  depend() {
    if (target && !this.subscribers.includes(target)) {
      this.subscribers.push(target);
    }
  }
  notify() {
    this.subscribers.forEach(sub => sub());
  }
}

观察者模式 通过 Watcher 监听数据变化并更新视图:

let target = null;
class Watcher {
  constructor(func) {
    target = func;
    target();
    target = null;
  }
}

基于 Proxy 的实现(Vue 3.x)

Vue 3 使用 Proxy 替代 Object.defineProperty,能直接监听对象而非属性,并支持数组索引修改:

实现vue双向绑定

function reactive(obj) {
  return new Proxy(obj, {
    get(target, key) {
      track(target, key);
      return target[key];
    },
    set(target, key, value) {
      target[key] = value;
      trigger(target, key);
      return true;
    }
  });
}

模板编译

Vue 将模板编译为渲染函数,解析指令如 v-model

  • v-model 本质是 :value 绑定和 @input 事件的语法糖:
    <input v-model="message">
    <!-- 等价于 -->
    <input :value="message" @input="message = $event.target.value">

实现简易双向绑定

完整示例代码:

// 数据劫持 + 发布订阅
class Dep {
  constructor() { this.subs = new Set(); }
  depend() { if (target) this.subs.add(target); }
  notify() { this.subs.forEach(sub => sub()); }
}

function observe(obj) {
  Object.keys(obj).forEach(key => {
    let val = obj[key];
    const dep = new Dep();
    Object.defineProperty(obj, key, {
      get() {
        dep.depend();
        return val;
      },
      set(newVal) {
        val = newVal;
        dep.notify();
      }
    });
  });
}

let target = null;
function watch(fn) {
  target = fn;
  fn();
  target = null;
}

// 使用示例
const data = { message: '' };
observe(data);

watch(() => {
  document.getElementById('input').value = data.message;
  document.getElementById('output').textContent = data.message;
});

document.getElementById('input').addEventListener('input', (e) => {
  data.message = e.target.value;
});

对比 Vue 2 与 Vue 3 实现差异

  • Vue 2:基于 Object.defineProperty,需递归遍历对象,无法检测数组索引变化。
  • Vue 3:基于 Proxy,可监听整个对象,支持数组变化检测,性能更好。

通过以上机制,Vue 实现了数据变化自动更新视图、视图输入自动更新数据的双向绑定效果。

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

相关文章

vue实现双向绑定的是

vue实现双向绑定的是

Vue 双向绑定实现原理 Vue 实现双向绑定的核心机制是数据劫持结合发布-订阅模式,主要通过 v-model 指令实现。以下是关键实现细节: 数据劫持(响应式系统) Vue 2.x 使用 Obj…

vue实现双向

vue实现双向

Vue 实现双向绑定的方法 Vue 主要通过 v-model 指令实现双向绑定,适用于表单元素或自定义组件。以下是几种常见实现方式: 表单元素的双向绑定 在表单元素(如 input、textarea…

jquery事件绑定

jquery事件绑定

jQuery 事件绑定的基本方法 使用 on() 方法绑定事件是最推荐的方式,兼容性强且支持动态元素。语法如下: $(selector).on(eventName, handlerFunction)…

vue实现绑定herf

vue实现绑定herf

在 Vue 中绑定 href 的方法 Vue 中可以通过 v-bind 指令或简写 : 动态绑定 href 属性,支持绑定静态值、动态变量或计算属性。 静态绑定 <a href="htt…

vue如何实现绑定

vue如何实现绑定

数据绑定基础 Vue 通过 v-bind 指令实现动态绑定 HTML 属性或组件 prop。基本语法为 v-bind:属性名="表达式",简写为 :属性名="表达式"。例如绑定 class 或 sty…

vue 双向绑定实现

vue 双向绑定实现

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