当前位置:首页 > VUE

vue 数组响应式实现

2026-02-22 19:50:07VUE

Vue 数组响应式实现原理

Vue 通过重写数组的原型方法实现对数组变化的监听。核心思路是拦截数组的变异方法(如 pushpopsplice 等),在方法执行前后触发依赖更新。

重写数组原型

Vue 创建了一个继承自 Array.prototype 的对象作为新的数组原型,并重写了其中会修改原数组的方法:

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

const methodsToPatch = [
  'push',
  'pop',
  'shift',
  'unshift',
  'splice',
  'sort',
  'reverse'
]

methodsToPatch.forEach(function (method) {
  const original = arrayProto[method]
  def(arrayMethods, method, function mutator (...args) {
    const result = original.apply(this, args)
    const ob = this.__ob__
    let inserted
    switch (method) {
      case 'push':
      case 'unshift':
        inserted = args
        break
      case 'splice':
        inserted = args.slice(2)
        break
    }
    if (inserted) ob.observeArray(inserted)
    ob.dep.notify()
    return result
  })
})

响应式处理入口

在初始化数据时,Vue 会遍历对象属性,对数组进行特殊处理:

vue 数组响应式实现

class Observer {
  constructor(value) {
    if (Array.isArray(value)) {
      value.__proto__ = arrayMethods
      this.observeArray(value)
    } else {
      this.walk(value)
    }
  }

  observeArray(items) {
    for (let i = 0, l = items.length; i < l; i++) {
      observe(items[i])
    }
  }
}

处理新增元素

对于可能新增元素的方法(pushunshiftsplice),需要对新元素进行响应式处理:

if (inserted) ob.observeArray(inserted)

触发更新

每个被观察的数组都有一个关联的 Dep 实例,当数组变化时通过 dep.notify() 通知所有订阅者:

vue 数组响应式实现

ob.dep.notify()

注意事项

直接通过索引修改数组元素或修改数组长度不会被检测到:

vm.items[index] = newValue // 非响应式
vm.items.length = newLength // 非响应式

应使用 Vue.set 或数组的变异方法:

Vue.set(vm.items, index, newValue)
vm.items.splice(index, 1, newValue)

总结

Vue 的数组响应式实现通过以下方式工作:

  • 创建继承自原生数组原型的变异版本
  • 重写会修改数组的7个方法
  • 方法执行后对新元素进行响应式处理
  • 通过依赖收集和通知机制触发视图更新
  • 提供 Vue.set 和变异方法作为修改数组的安全方式

标签: 数组vue
分享给朋友:

相关文章

vue实现语音

vue实现语音

Vue 实现语音功能的方法 使用 Web Speech API Vue 可以结合 Web Speech API 实现语音识别和合成功能。Web Speech API 包含 SpeechRecognit…

甘特图vue实现

甘特图vue实现

甘特图 Vue 实现方法 使用开源库 vue-ganttastic 安装依赖: npm install vue-ganttastic 基础实现代码: <template> <…

vue实现心电图

vue实现心电图

Vue 实现心电图效果 在 Vue 中实现心电图效果通常需要结合 Canvas 或 SVG 进行动态绘图,模拟心电图数据的实时变化。以下是两种实现方式的核心思路和代码示例: 使用 Canvas 绘制…

vue toast实现

vue toast实现

Vue Toast 实现方法 使用第三方库(推荐) 安装 vue-toastification 库,这是一个功能丰富且轻量级的 Vue Toast 插件。 npm install vue-toast…

vue实现github

vue实现github

Vue 实现 GitHub 相关功能 创建 GitHub API 请求 在 Vue 项目中,可以使用 axios 或 fetch 与 GitHub API 进行交互。需要在 GitHub 上创建个人访…

vue实现CRUD

vue实现CRUD

Vue 实现 CRUD 操作 Vue 提供了响应式数据绑定和组件化开发能力,结合后端 API 可以轻松实现 CRUD(增删改查)功能。以下是基于 Vue 3 和 Axios 的完整实现方案。 安装…