当前位置:首页 > VUE

vue响应式实现机制

2026-02-23 18:33:04VUE

Vue 响应式实现机制

Vue 的响应式系统通过数据劫持和依赖收集实现,核心基于 Object.defineProperty(Vue 2)或 Proxy(Vue 3)。以下是具体实现机制:

数据劫持

Vue 2 使用 Object.defineProperty 劫持对象的属性访问和修改。通过定义 gettersetter,在属性被访问或修改时触发依赖更新。

vue响应式实现机制

// Vue 2 数据劫持简化示例
function defineReactive(obj, key, val) {
  Object.defineProperty(obj, key, {
    get() {
      console.log('get', key);
      return val;
    },
    set(newVal) {
      if (newVal !== val) {
        console.log('set', key);
        val = newVal;
      }
    }
  });
}

Vue 3 改用 Proxy 代理整个对象,能直接监听对象增删和数组变化,无需递归遍历属性。

// Vue 3 Proxy 示例
const reactive = (target) => {
  return new Proxy(target, {
    get(target, key, receiver) {
      console.log('get', key);
      return Reflect.get(target, key, receiver);
    },
    set(target, key, value, receiver) {
      console.log('set', key);
      return Reflect.set(target, key, value, receiver);
    }
  });
};

依赖收集

每个响应式数据关联一个 Dep 实例(依赖管理器),在 getter 中收集当前正在执行的 Watcher(依赖),在 setter 中通知所有 Watcher 更新。

vue响应式实现机制

class Dep {
  constructor() {
    this.subscribers = new Set();
  }
  depend() {
    if (activeWatcher) this.subscribers.add(activeWatcher);
  }
  notify() {
    this.subscribers.forEach(watcher => watcher.update());
  }
}

let activeWatcher = null;
class Watcher {
  constructor(updateFn) {
    this.updateFn = updateFn;
    this.update();
  }
  update() {
    activeWatcher = this;
    this.updateFn();
    activeWatcher = null;
  }
}

虚拟 DOM 与批量更新

数据变化触发 Watcher 更新后,Vue 通过异步队列(nextTick)批量处理 DOM 更新,避免重复渲染。虚拟 DOM 差异比对(diff 算法)最小化真实 DOM 操作。

数组响应式处理

Vue 2 通过重写数组的 pushpop 等方法实现响应式,内部调用 ob.dep.notify() 通知更新。Vue 3 的 Proxy 可直接监听数组索引变化。

// Vue 2 数组劫持示例
const arrayProto = Array.prototype;
const arrayMethods = Object.create(arrayProto);
['push', 'pop'].forEach(method => {
  const original = arrayProto[method];
  arrayMethods[method] = function(...args) {
    const result = original.apply(this, args);
    this.__ob__.dep.notify();
    return result;
  };
});

总结

  • Vue 2:基于 Object.defineProperty + Dep/Watcher,需递归遍历对象且对数组特殊处理。
  • Vue 3:基于 Proxy + effect(类似 Watcher),支持原生对象和数组操作,性能更优。

标签: 机制vue
分享给朋友:

相关文章

vue实现流程

vue实现流程

Vue 实现流程的基本步骤 安装 Vue.js 可以通过 CDN 引入或使用 npm/yarn 安装。 CDN 方式: <script src="https://cdn.jsdelivr.…

通过vue实现

通过vue实现

Vue 实现方法 安装 Vue 确保已安装 Node.js 和 npm,通过以下命令安装 Vue CLI: npm install -g @vue/cli 创建 Vue 项目 使用 Vue CLI…

vue实现swiper

vue实现swiper

Vue 中实现 Swiper 的方法 安装 Swiper 依赖 在 Vue 项目中安装 Swiper 和相关依赖: npm install swiper vue-awesome-swiper 全局引…

vue实现书库

vue实现书库

Vue 实现书库功能 使用 Vue 构建书库功能需要结合前端框架和后端数据管理。以下是实现步骤和关键点: 项目初始化 使用 Vue CLI 或 Vite 初始化项目,安装 Vue Router 和状…

vue实现刷新

vue实现刷新

Vue 实现页面刷新的方法 在Vue中实现页面刷新可以通过多种方式,以下是几种常见的方法: 使用 window.location.reload() 通过调用浏览器的原生方法强制刷新当前页面:…

vue实现双击

vue实现双击

Vue 实现双击事件的方法 在Vue中实现双击事件可以通过以下几种方式完成,具体选择取决于项目需求和开发习惯。 使用 @dblclick 指令 Vue提供了内置的@dblclick指令,可以直接在模…