Vue 响应式实现原理

2023-11-13

准备工作

  • 数据驱动
  • 响应式的核心原理
  • 发布订阅模式和观察者模式

数据驱动

  • 数据响应式、双向绑定、数据驱动
    • 数据响应式
      • 数据模型仅仅是普通的 JS 对象,而当我们修改数据时,试图回进行更新,避免了繁琐的 DOM 操作,提高开发效率
    • 双向绑定
      • 数据改变,视图改变;视图改变,数据也随之改变
      • 我们可以使用 v-model 在表单元素上创建双向数据绑定
    • 数据驱动是 Vue 最独特的特性之一
      • 开发过程中仅需关注数据本身,不需要关心数据是如何渲染到视图

响应式的核心原理

vue2

浏览器兼容 IE8 以上(不兼容 IE8)

  • 单个属性
// 模拟 vue 中的 data 选项
let data = {
  msg: 'hello'
}

// 模拟 vue 的实例
let vm = {}

// 数据劫持:当访问或者设置 vm 中的成员的时候,做一些干预操作
object.defineProperty(vm, 'msg', {
  // 可枚举(可遍历)
  enumerable: true,
  // 可配置(可以使用 delete 删除,可以通过 defineProperty 重新定义)
  configurable: true,
  // 当获取值的时候执行
  get () {
    console.log('get', data.msg)
    return data.msg
  },
  // 当设置值的时候执行
  set (newValue) {
    console.log('set', newValue)
    if (newValue === data.msg) {
      return
    }
    data.msg = newValue
    // 数据更改,更新 DOM 的值
    document.querySelector('#app').textContent = data.msg
  }
})

// 测试
vm.msg = 'Hello World'
console.log(vm.msg)
  • 多个属性
// 模拟 vue 中的 data 选项
let data = {
  msg: 'hello',
  count: 10
}

// 模拟 vue 的实例
let vm = {}

proxyData(data)

function proxyData(data) {
  // 遍历 data 对象的所有属性
  Object.keys(data).forEach(key => {
    // 把 data 中的属性,转换成 vm 的 setter/getter
    Object.defineProperty(vm, key, {
      enumerable: true,
      configurable: true,
      get () {
        console.log('get', data.msg)
        return data[key]
      },
      set (newValue) {
        console.log('set', newValue)
        if (newValue === data[key]) return
        data[key] = newValue
        document.querySelector('#app').textContent = data[key]
      }
    })
  })
}

// 测试
vm.msg = 'Hello World'
console.log(vm.msg)

vue3

直接监听对象,而非属性。
ES 6中新增,IE 不支持,性能由浏览器优化

// 模拟 vue 中的 data 选项
let data = {
  msg: 'hello',
  count: 0
}

// 模拟 vue 实例
let vm = new Proxy(data, {
  // 当访问 vm 的成员会执行
  get (target, key) {
    console.log('get, key:', key, target[key])
    return target[key]
  }
  // 当设置 vm 的成员会执行
  set (target, key, newValue) {
    console.log('set, key:', key, newValue)
    if (target[key] === newValue) return
    target[key] = newValue
    document.querySelector('#app').textContent = target[key]
  }
})
// 测试
vm.msg = 'Hello World'
console.log(vm.msg)

发布订阅模式和观察者模式

发布订阅

  • 订阅者
  • 发布者
  • 信号中心

什么是 “发布/订阅模式” (publish-subscribe pattern)

  • 我们假定,存在一个 “信号中心”,某个任务执行完成,就向信号中心 “发布”(publish) 一个信号,其他任务可以向信号中心 “订阅”(subscribe) 这个信号,从而知道什么时候自己可以开始执行

一家超市,存在一个公众号,或者小程序,也就是上述的 “信号中心”

每当这家超市出现折扣,就向小程序或者公众号发布活动信息,也就是上述的 “向信号中心 发布 一个信号”

这家超市的会员可以关注小程序或者公众号,这样在超市发布活动时,自己就知道什么时候去薅羊毛,也就是上述的 “其他任务 向 信号中心 订阅 这个信号,从而知道自己什么时候可以开始执行”

Vue 的自定义事件

let vm = new Vue()

vm.$on('dataChange', () => {
  console.log('dataChange')
})

vm.$on('dataChange', () => {
  console.log('dataChange1')
})

vm.$emit('dataChange')
  • 这段代码中我们无法直观地感受到事件发布与订阅,所以我们可以借助下边的场景来更直观的感受发布与订阅模式

兄弟组件通信过程

// eventBus.js
// 事件中心
let eventHub = new Vue()

// ComponentA.vue --- 发布者
addTodo: function () {
  // 发布消息(事件)
  eventHub.$emit('add-todo', {text: this.newTodoText})
  this.newTodoText = ''
}

// ComponentB.vue --- 订阅者
created: function () {
  // 订阅消息
  eventHub.$on('add-todo', this.addTodo)
}
  • 通过 eventBus 我们就可以感受到事件的订阅与发布
  • 这段代码中 ComponentA 就相当于上述举例的 超市的小程序或者公众号 ,ComponentB 就相当于上述举例的 关注了这家超市公众号的会员
  • 发布者 通过 $emit 触发了一个叫做 add-todo 的事件后,那么通过 $on 订阅了 add-todo 的订阅者,就会执行对应的事件

模拟 Vue 自定义事件的实现

class EventEmitter {
  constructor () {
    // { eventType: [handler1, handler2] }
    this.subs = {}
  }

  // 订阅通知
  $on (eventType, handler) {
    this.subs[eventType] = this.subs[eventType] || []
    this.subs[eventType].push(handler)
  }

  // 发布通知
  $emit (eventType) {
    if (this.subs[eventType]) {
      this.subs[eventType].forEach(handler => {
        handler()
      })
    }
  }
}

// 测试
var bus = new EventEmitter()

// 注册事件
bus.$on('click', function () {
  console.log('click')
})

bus.$on('click', function () {
  console.log('click1')
})

// 触发事件
bus.$emit('click')

观察者

  • 观察者(订阅者)— Watcher
    • update():当事件发生时,具体要做的事情
  • 目标(发布者)— Dep
    • subs 数组:存储所有的观察者
    • addSub():添加观察者
    • notify():当事件发生,调用所有观察者的 update() 方法
  • 没有事件中心
// 目标(发布者)
// Dependency
class Dep {
  constructor () {
    // 存储所有的观察者
    this.subs = []
  }
  // 添加观察者
  addSub (sub) {
    if (sub && sub.update) {
      this.subs.push(sub)
    }
  }
  // 通知所有观察者
  notify () {
    this.subs.forEach(sub => {
      sub.update()
    })
  }
}

// 观察者(订阅者)
class Watcher {
  update () {
    console.log('update')
  }
}

// 测试
let dep = new Dep()
let watcher = new Watcher()
dep.addSub(watcher)
dep.notify()

总结

  • 观察者模式 是由具体目标调度,比如当事件触发,Dep 就回去调用观察者的方法,所以观察者模式的订阅者与发布者之间是存在依赖的
  • 发布/订阅模式 是由统一调度中心调用,因此发布者和订阅者不需要知道对方的存在

在这里插入图片描述

Vue 响应式原理模拟

整体分析

  • Vue 基本结构
  • 打印 Vue 实例观察
  • 整体结构

在这里插入图片描述

  • Vue
    • 把 data 中的成员注入到 Vue 实例,并且把 data 中的成员转成 getter/setter
  • Observer
    • 能够把数据对象的所有属性进行监听,如有变动可拿到最新值并通知 Dep
  • Compiler
    • 解析每个元素中的指令/插值表达式,并替换成相应的数据
  • Dep
    • 添加观察者(watcher),当数据变化通知所有观察者
  • Watcher
    • 数据变化更新视图

Vue 类

  • 功能
    • 接收初始化的参数(选项)
    • 把 data 中的属性注入到 Vue 实例,转换成 getter/setter
    • 调用 observer 监听 data 中所有属性的变化
    • 调用 compiler 解析指令/插值表达式
  • 结构
    • $options
    • $el
    • $data
    • _proxyData() 静态方法不对外暴露
  • 代码
    class Vue {
      constructor (options) {
        // 1. 保存选项的数据
        this.$options = options || {}
        this.$data = options.data || {}
        const el = options.el
        this.$el = typeof options.el === 'string' ? document.querySelector(el) : el
    
        // 2. 负责把 data 注入到 Vue 实例
        this._proxyData(this.$data)
    
        // 3. 负责调用 Oberver 实现数据劫持
        // 4. 负责调用 Compiler 解析指令/插值表达式等
      }
    
      _proxyData (data) {
        // 遍历 data 的所有属性
        Object.keys(data).forEach(key => {
          Object.defineProperty(this, key, {
            enumerable: true,
            configurable: true,
            get () {
              return data[key]
            },
            set (newValue) {
              if (data[key] == newValue) return
              data[key] = newValue
          })
        })
      }
    }
    

Observer 类

  • 功能
    • 把 data 选项中的属性转换成响应式数据
    • data 中的某个属性也是对象,把该属性转换成响应式数据
    • 数据变化发送通知
  • 结构
    • walk(data)
    • defineReactive(data, key, value)
  • 代码
    // 负责数据劫持
    // 把 $data 中的成员转换成 getter/setter
    class Observer {
      constructor (data) {
        this.walk(data)
      }
      // 1. 判断数据是否是对象,如果不是对象返回
      // 2. 如果是对象,遍历对象的所有属性,设置为 getter/setter
      walk (data) {
        if (!data || typeof data !== 'object') return
        // 遍历 data 的所有成员
        Object.keys(data).forEach(key => {
          this.defineReactive(data, key, data[key])
        })
      }
      // 定义响应式成员
      defineReactive (data, key, val) {
        const that = this
        // 如果 val 是对象,继续设置它下面的成员为响应式数据
        this.walk(val)
        Object.defineProperty(data, key, {
          configurable: true,
          enumerable: true,
          get () {
            return val
          },
          set (newValue) {
            if (newValue === val) return
            // 如果 newValue 是对象,设置 newValue 的成员为响应式
            this.walk(newValue)
            val = newValue
          }
        })
      }
    }
    

Compiler 类

  • 功能
    • 负责编译模板,解析指令/插值表达式
    • 负责页面的首次渲染
    • 当数据变化后重新渲染视图
  • 结构
    • 属性
      • el
      • vm
    • 方法
      • compile(el)
      • compileElement(node)
      • compileText(node)
      • isDirective(attrName)
      • isTextNode(node)
      • isElementNode(node)
  • 代码
    • compile
      • 负责解析指令/插值表达式
    // 负责解析指令/插值表达式
    class Compiler {
      constructor (vm) {
        this.vm = vm
        this.el = vm.$el
        // 编译模板
        this.compile(this.el)
      }
      // 编译模板
      // 处理文本节点和元素节点
      compile (el) {
        const nodes = el.childNodes
        Arrat.from(nodes).forEach(node => {
          // 判断是文本节点还是元素节点
          if (this.isTextNode(node)) {
            this.compileText(node)
          } else if (this.isElementNode(node)) {
            this.compileElement(node)
          }
    
          if (node.childNodes && node.childNodes.length) {
            // 如果当前节点中还有子节点,递归编译
            this.compile(node)
          }
        })
      }
      // 判断是否是文本节点
      isTextNode (node) {
        return node.nodeType === 3
      }
      // 判断是否是节点属性
      isElementNode (node) {
        return node.nodeType === 1
      }
      // 判断是否是以 v- 开头的指令
      isDirective (attrName) {
        return attrName.startswith('v-')
      }
      // 编译文本节点
      compileText (node) {
    
      }
      // 编译属性节点
      compileElement (node) {
    
      }
    }
    
    • compileElement
      • 负责编译元素的指令
      • 处理 v-text 的首次渲染
      • 处理 v-model 的首次渲染
    // 编译属性节点
    compileElement (node) {
      // 遍历元素节点中的所有属性,找到指令
      Array.form(node.attributes).forEach(attr => {
        // 获取元素属性的名称
        let attrName = attr.name
    
        // 判断当前的属性名称是否是指令
        if (this.isDirective(attrName)) {
          // attrName 的形式 v-text v-model
          // 截取属性的名称,获取 text model
          attrName = attrName.substr(2)
          // 获取属性的名称,属性的名称就是我们数据对象的属性 v-text='name',获取的是 name
          const key = attr.value
          // 处理不同的指令
          this.update(node, key, attrName)
        }
      })
    }
    
    // 负责更新 DOM
    // 创建 watcher
    update (node, key, dir) {
      // node 节点,key 数据的属性名称,dir 指令的前半部分
      const updaterFn = this[dir + 'Updater']
      updaterFn && updaterFn(node, this.vm[key])
    }
    
    // v-text 指令的更新方法
    textUpdater (node, value) {
      node.textContent = value
    }
    // v-model 指令的更新方法
    modelUpdater (node, value) {
      node.value = value
    }
    
    • compileText
      • 负责编译插值表达式
    // 编译文本节点
    compileText (node) {
      const reg = /\{\{(.+?)\}\}/
      // 获取文本节点的内容
      const value = node.textContent
      if (reg.test(value)) {
        // 插值表达式中的值就是我们要的属性名称
        const key = RegExp.$1.trim()
        // 把插值表达式替换成具体的值
        node.textContent = value.replace(reg, this.vm[key])
      }
    }
    
    • isDirective
      • 判断元素属性名是否为指令,以 v- 开头都是指令
    isDirective(attrName) {
      return attrName.startsWith('v-')
    }
    
    • isTextNode
      • 判断是否是文本节点
    isTextNode (node) {
      return node.nodeType === 3
    }
    
    • isElementNode
      • 判断是否是元素节点
    isElementNode (node) {
      return node.nodeType === 1
    }
    

Dep(Dependency) 类

在这里插入图片描述

  • 功能
    • 收集依赖,添加观察者(watcher)
    • 通知所有观察者
  • 结构
    • subs
    • addSub(sub)
    • notify
  • 代码
class Dep {
  constructor () {
    // 存储所有的观察者
    this.subs = []
  }
  // 添加观察者
  addSub (sub) {
    if (sub && sub.update) {
      this.subs.push(sub)
    }
  }
  // 通知所有观察者
  notify () {
    this.subs.forEach(sub => {
      Sub.update()
    })
  }
}
  • 在 compiler.js 中收集依赖,发送通知
// defineReactive 中
// 创建 dep 对象收集依赖
const dep = new Dep()

// getter 中 --- get 的过程中收集依赖
Dep.target && dep.addSub(Dep.target)

// setter 中 --- 当数据变化制后,发送通知
dep.notify()

Watcher 类

在这里插入图片描述

  • 功能
    • 当数据变化触发依赖,dep通知所有的 Watcher 实例更新视图
    • 自身实例化的时候往 dep 对象中添加自己
  • 结构
    • vm
    • key
    • cb
    • oldValue
    • update()
  • 代码
class watcher {
  constructor (vm, key, cb) {
    this.vm = vm
    // data 中的属性名称
    this.key = key
    // 当数据变化的时候,调用 cb 更新视图
    this.cb = cb
    // 在 Dep 的静态属性上记录当前 watcher 对象,当访问数据的时候把 watcher 添加到 dep 的 subs 中
    Dep.target = this
    // 触发一次 getter,让 dep 为当前 key 记录 watcher
    this.oldValue = vm[key]
    // 清空 target
    Dep.target = null
  }
  update () {
    const newValue = this.vm[this.key]
    if (this.oldValue === newValue) return
    this.cb(newValue)
  }
}
  • 在 compiler.js 中为每一个指令 / 插值表达式创建 watcher 对象,监视数据的变化
update(node, key, attrName) {
  let updateFn = this[attrName + 'Updater']
  // 因为在 textUpdater 等方法中要使用 this
  updateFn && updateFn.call(this, node, this.vm[key], key)
}

// v-text 指令的更新方法
textUpdater (node, value, key) {
  node.textContent = value
  // 每一个指令中创建一个 watcher,观察数据的变化
  new Watcher(this.vm, key, value => {
    node.textContent = value
  })
}

总结

在这里插入图片描述

  • Vue
    • 记录传入的选项,设置 $data/$el
    • 把 data 的成员注入到 Vue 实例
    • 负责调用 Observer 实现数据响应式处理(数据劫持)
    • 负责调用 Compiler 编译指令 / 插值表达式等
  • Observer
    • 数据劫持
      • 负责把 data 中的成员转换成 getter/setter
      • 负责把多层属性转换成 getter/setter
      • 如果给属性赋值为新对象,把新对象的成员设置为 getter/setter
    • 添加 Dep 和 Watcher 的依赖关系
    • 数据变化发送通知
  • Compiler
    • 负责编译模板,解析指令/插值表达式
    • 负责页面的首次渲染过程
    • 当数据变化后重新渲染
  • Dep
    • 收集依赖,添加订阅者(watcher)
    • 通知所有订阅者
  • Watcher
    • 自身实例化的时候往 dep 对象中添加自己
    • 当数据变化 dep 通知所有的 Watcher 实例更新视图
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Vue 响应式实现原理 的相关文章

随机推荐

  • /usr/bin/ld: cannot find -lmysqlcllient

    文章目录 1 question usr bin ld cannot find lmysqlcllient 2 solution 1 question usr bin ld cannot find lmysqlcllient 2 soluti
  • Unity脚本的属性

    参考官网 http game ceeger com Script Attributes Attributes html http blogs unity3d com 2014 06 24 serialization in unity 参考文
  • 解决UE4启动出现UE4Editor.exe-无法找到dll入口的弹窗

    UE4编辑器启动 一开始遇到的问题如下 上网找问题得到的解答都是在cmd下利用regsvr32 exe注册该dll到注册表 但是也提示报错 上网搜了一下 得知原因是生成该dll的源码没有实现 DllRegisterServer和DllUne
  • 镜像iso文件下载地址

    CentOS 7官方下载地址 https www centos org download Centos国内下载源 以下链接均可下载镜像文件 http man linuxde net download CentOS http centos u
  • 面向对象程序设计语言(Java)-1.概述

    概述 1 Java的两层含义 2 Java语言的特点 3 Java的应用平台 4 Java的工作原理 5 Java环境中的概念 6 初始Java程序 7 Java程序的基本组成 8 开发Java程序的步骤 9 注释 1 Java的两层含义
  • JavaScript中的扁平化数据转换为树形结构、树形结构扁平化数据

    1 扁平化数据 gt 树形结构 1 1 第一种数据类型 原始数据只有id和pId相互关联 let data id 639 name 商品管理 type 0 pId 638 code 1 domain id 640 name 商品分类 typ
  • antdv(vue)组件中tree-select使用

    官网教程 组件tree select 实现效果 1 基本用法 直接使用 在vue层写数据 注意 注册组件要包含treeSelect和其中的节点ATreeSelectNode 不注册会报错 如下
  • Oracle常用代码总结

    1 用户 创建用户 create user dm identified by dm default tablespace BIGDATA DM temporary tablespace DM TEMP profile default 修改用
  • Centos7 搭建 Minikube

    Centos7 搭建 Minikube 目录 Centos7 搭建 Minikube 参考博客 运行环境 安装过程 配置系统环境 安装Docker 安装Kubectl 参考博客 参考博客 运行环境 系统版本 CentOS Linux rel
  • Flutter 页面中添加水印、自定义水印

    最近开发手机APP 使用 Fltter 由于需要使用水印的功能 但是第一次接触Flutter 就想着能不能在网上找到现成的使用 结果全是一群复制粘贴的 还卵用没有 不过由于我太机制 直接去官方的 pub get 找到一个插件 嘿嘿 pub
  • 教你统计日留存、周留存、月留存率更准确的方法。

    2019独角兽企业重金招聘Python工程师标准 gt gt gt 什么是留存用户 某段时间内的新增用户 经过一段时间后 仍继续使用应用的被认作是留存用户 这部分用户占当时新增用户的比例即是留存率 统计留存用户的时间粒度有哪些 自然日 包括
  • C/C++宏编程

    C C 宏编程 宏的复杂使用 永远不要写两次 介绍 我读过的所有C C 教科书都批评宏的使用 不要使用它们 它们很危险 因为它们隐藏了你实际写的东西 尤其是看起来很实用的宏 有些人甚至说 没有理由在C 的模板类的发明中使用宏 尽管如此 宏仍
  • centos7 mysql启动失败_RPM方式安装MySQL

    RPM方式安装MySQL 最近浪子尝试使用mycat做MySQL的读写分离和分表分库 因此搭建了几台虚拟机来做操作 话不多说 我们现在centos7上安装MySQL 据说centos7上面直接用yum的方式安装MySQL会失败 那么我就直接
  • 与或非逻辑符号_数电学习之 逻辑电路(1)

    先导 逻辑图的表示 1 与或非 01 02 03 2 扩展 异或 不同为1 相同为0 和同或 相同为1 不同为0 3 复合运算 与非 与后面加一个小圆圈 或非 或后面加一个小圆圈 与或非 两个与输入到或中 或后面加一个小圆圈 4 逻辑公式
  • Qtcreator中来调用python的函数的用法

    以下内容是参考博客 https blog csdn net alxe made article details 83382159 由以上大神的博客作为参考成功实现的 一 先说几点注意的地方 1 就是需要将python的路径在pro中加载进来
  • 和利时系统如何下装服务器,和利时服务器如何将A设置B

    和利时服务器如何将A设置B 内容精选 换一换 系统盘镜像和数据盘镜像为128个 整机镜像为10个 没有限制 可以 支持中国站和国际站的帐号之间共享镜像 但是仅限于中国站和国际站共同拥有的区域 例如 您在中国站的 华北 北京四 的镜像不能共享
  • ShardingSphere报错-java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

    目录 一 场景 二 报错信息 三 排查 四 原因 五 解决 一 场景 1 项目使用ShardingJDBC操作数据库 2 查询SQL执行报错 但将sql复制到navicat中执行 是正常的 二 报错信息 nested exception i
  • 2021-06-15

    com aspose diagram afr Unexcepted eof 有没有大佬遇到过这个问题 救命
  • 华为OD机试 Java 几何平均值最大子数组

    题目 代码 import java util public class MaxGeometricMean public static void main String ar
  • Vue 响应式实现原理

    准备工作 数据驱动 响应式的核心原理 发布订阅模式和观察者模式 数据驱动 数据响应式 双向绑定 数据驱动 数据响应式 数据模型仅仅是普通的 JS 对象 而当我们修改数据时 试图回进行更新 避免了繁琐的 DOM 操作 提高开发效率 双向绑定