当前位置:首页 > VUE

Vue实现数据响应式

2026-02-25 07:43:34VUE

Vue 数据响应式原理

Vue 的数据响应式是通过 数据劫持 结合 发布-订阅模式 实现的,核心是利用 Object.defineProperty(Vue 2.x)或 Proxy(Vue 3.x)来监听数据变化。

Vue 2.x 实现方式

使用 Object.defineProperty

Vue 2.x 通过 Object.defineProperty 递归地将数据对象的属性转换为 getter/setter,从而在属性被访问或修改时触发更新。

function defineReactive(obj, key, val) {
  Object.defineProperty(obj, key, {
    enumerable: true,
    configurable: true,
    get: function() {
      console.log(`获取 ${key}: ${val}`);
      return val;
    },
    set: function(newVal) {
      if (newVal !== val) {
        console.log(`设置 ${key}: ${newVal}`);
        val = newVal;
        // 触发视图更新
      }
    }
  });
}

const data = { name: 'Vue' };
defineReactive(data, 'name', data.name);
data.name = 'React'; // 触发 setter

数组的特殊处理

Vue 2.x 无法直接监听数组索引变化,因此重写了数组的 pushpopshiftunshiftsplicesortreverse 方法,在调用这些方法时触发更新。

Vue实现数据响应式

const arrayProto = Array.prototype;
const arrayMethods = Object.create(arrayProto);

['push', 'pop', 'shift', 'unshift', 'splice', 'sort', 'reverse'].forEach(method => {
  const original = arrayProto[method];
  arrayMethods[method] = function(...args) {
    const result = original.apply(this, args);
    console.log(`数组方法 ${method} 被调用`);
    // 触发视图更新
    return result;
  };
});

Vue 3.x 实现方式

使用 Proxy

Vue 3.x 改用 Proxy 实现响应式,可以直接监听对象和数组的变化,无需特殊处理。

function reactive(obj) {
  return new Proxy(obj, {
    get(target, key) {
      console.log(`获取 ${key}: ${target[key]}`);
      return target[key];
    },
    set(target, key, value) {
      if (target[key] !== value) {
        console.log(`设置 ${key}: ${value}`);
        target[key] = value;
        // 触发视图更新
      }
      return true;
    }
  });
}

const data = reactive({ name: 'Vue' });
data.name = 'React'; // 触发 set

响应式工具函数

Vue 3.x 提供了 reactive(对象)和 ref(基本类型)等 API:

Vue实现数据响应式

import { reactive, ref } from 'vue';

const state = reactive({ count: 0 });
const num = ref(1);

state.count++; // 自动触发更新
num.value++;   // 通过 .value 访问

依赖收集与派发更新

依赖收集(Dep)

在 getter 中收集依赖(Watcher),在 setter 中通知依赖更新。

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

观察者(Watcher)

每个组件实例对应一个 Watcher,负责更新视图。

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

总结

  • Vue 2.x:通过 Object.defineProperty 监听对象属性,重写数组方法。
  • Vue 3.x:通过 Proxy 实现全面监听,支持对象和数组。
  • 核心流程:数据劫持 → 依赖收集 → 派发更新 → 视图渲染。

标签: 数据Vue
分享给朋友:

相关文章

vue实现数据结构图

vue实现数据结构图

Vue 实现数据结构图的方法 使用第三方库 Vue 可以与多种图表库结合使用,例如 D3.js、ECharts 或 Vis.js。这些库提供了强大的图形渲染能力,适合展示复杂的数据结构图。 安装 E…

Vue实现画布

Vue实现画布

Vue 实现画布的方法 在 Vue 中实现画布功能可以通过 HTML5 的 <canvas> 元素结合 Vue 的响应式特性来完成。以下是几种常见的实现方式: 使用原生 <can…

Vue 实现登出

Vue 实现登出

Vue 实现登出的方法 在 Vue 应用中实现登出功能通常涉及清除用户认证信息、重定向到登录页面或主页,并可能调用后端 API 注销会话。以下是几种常见的实现方式: 清除本地存储的认证信息 大多数…

Vue实现路由监

Vue实现路由监

Vue 实现路由监听的方法 在 Vue 中可以通过多种方式实现路由监听,以下是常见的几种方法: 使用 watch 监听 $route 对象 通过 Vue 的 watch 功能可以监听 $route…

Vue实现聊天软件

Vue实现聊天软件

Vue实现聊天软件的关键步骤 环境准备与项目初始化 使用Vue CLI或Vite创建Vue 3项目,安装必要依赖如vue-router、pinia(状态管理)、socket.io-client(实时通…

vue 实现数据分页

vue 实现数据分页

实现数据分页的基本思路 在Vue中实现数据分页通常需要结合前端分页逻辑和后端API支持。前端分页适合数据量较小的情况,后端分页适合大数据量场景。 前端分页实现 创建一个计算属性来分割当前页数据:…