欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue中響應(yīng)式系統(tǒng)實(shí)現(xiàn)原理圖文講解

 更新時(shí)間:2023年03月20日 11:53:05   作者:kgduu  
Vue的響應(yīng)式實(shí)現(xiàn)是借助Object.defineProperty通過重寫getter和setter方法來進(jìn)行的數(shù)據(jù)劫持,Vue3通過Proxy代理攔截對象中任意屬性的變化,通過Reflect反射對源對象的屬性進(jìn)行操作,然后再在get里收集依賴在set里派發(fā)更新

框架

每個(gè)組件實(shí)例都會(huì)對應(yīng)一個(gè)watcher實(shí)例,在組件渲染過程中把接觸過的數(shù)據(jù)property記錄為依賴。當(dāng)依賴項(xiàng)的setter觸發(fā)時(shí),會(huì)通知watcher,重新渲染組件

vue實(shí)例中的data選項(xiàng),在初始化時(shí)會(huì)遍歷該對象所有屬性,并使用Object.defineProperty轉(zhuǎn)為getter/setter。

組件結(jié)構(gòu)

Observer:主要是將對象轉(zhuǎn)為響應(yīng)式對象,在getter中,會(huì)創(chuàng)建Dep來收集依賴

Dep:用于存放watcher,即副作用函數(shù)effectFn

Watcher:觀察者,當(dāng)有數(shù)據(jù)更新時(shí),會(huì)觸發(fā)update

源碼解析

在哪里綁定vue實(shí)例與watcher實(shí)例

在src/core/instance/lifecycle.ts中的mountComponent中

export function mountComponent(
  vm: Component,
  el: Element | null | undefined,
  hydrating?: boolean
): Component {
  vm.$el = el
  callHook(vm, 'beforeMount')
  let updateComponent
  /* istanbul ignore if */
  updateComponent = () => {
    vm._update(vm._render(), hydrating)
  }
  const watcherOptions: WatcherOptions = {
    before() {
      if (vm._isMounted && !vm._isDestroyed) {
        callHook(vm, 'beforeUpdate')
      }
    }
  }
  // we set this to vm._watcher inside the watcher's constructor
  // since the watcher's initial patch may call $forceUpdate (e.g. inside child
  // component's mounted hook), which relies on vm._watcher being already defined
  new Watcher(
    vm,
    updateComponent,
    noop,
    watcherOptions,
    true /* isRenderWatcher */
  )
  hydrating = false
  // flush buffer for flush: "pre" watchers queued in setup()
  const preWatchers = vm._preWatchers
  if (preWatchers) {
    for (let i = 0; i < preWatchers.length; i++) {
      preWatchers[i].run()
    }
  }
  // manually mounted instance, call mounted on self
  // mounted is called for render-created child components in its inserted hook
  if (vm.$vnode == null) {
    vm._isMounted = true
    callHook(vm, 'mounted')
  }
  return vm
}

vue實(shí)例中的data響應(yīng)式初始化

在src/core/instance/state.ts/initData中,主要是observe(data)這個(gè)

function initData(vm: Component) {
  let data: any = vm.$options.data
  data = vm._data = isFunction(data) ? getData(data, vm) : data || {}
  if (!isPlainObject(data)) {
    data = {}
  }
  // proxy data on instance
  const keys = Object.keys(data)
  const props = vm.$options.props
  const methods = vm.$options.methods
  let i = keys.length
  while (i--) {
    const key = keys[i]
    if (props && hasOwn(props, key)) {
 
    } else if (!isReserved(key)) {
      proxy(vm, `_data`, key)
    }
  }
  // observe data
  const ob = observe(data)
  ob && ob.vmCount++
}

對Object的變化監(jiān)測

Observer只追蹤數(shù)據(jù)是否被修改,無法追蹤新增和刪除屬性

Vue提供了Vue.prototype.$set和Vue.prototype.$delete,內(nèi)部也是調(diào)用Observer的方法

import {
  set,
  del
} from '../observer/index'
Vue.prototype.$set = set
Vue.prototype.$delete = del

到此這篇關(guān)于Vue中響應(yīng)式系統(tǒng)實(shí)現(xiàn)原理圖文講解的文章就介紹到這了,更多相關(guān)Vue響應(yīng)式系統(tǒng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論