Vue中響應(yīng)式系統(tǒng)實(shí)現(xiàn)原理圖文講解
框架
每個(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)文章
Vue表單綁定的實(shí)例代碼(單選按鈕,選擇框(單選時(shí),多選時(shí),用 v-for 渲染的動(dòng)態(tài)選項(xiàng))
本文通過實(shí)例代碼給大家介紹了Vue表單綁定(單選按鈕,選擇框(單選時(shí),多選時(shí),用 v-for 渲染的動(dòng)態(tài)選項(xiàng))的相關(guān)知識,非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-05-05Vuex的插件vuex-persistedstate數(shù)據(jù)持久化存儲操作
這篇文章主要介紹了Vuex的插件vuex-persistedstate數(shù)據(jù)持久化存儲操作,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-12-12vue中echarts圖表大小適應(yīng)窗口大小且不需要刷新案例
這篇文章主要介紹了vue中echarts圖表大小適應(yīng)窗口大小且不需要刷新案例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07vue-router實(shí)現(xiàn)tab標(biāo)簽頁(單頁面)詳解
這篇文章主要為大家詳細(xì)介紹了vue-router實(shí)現(xiàn)tab標(biāo)簽頁的相關(guān)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10vue3實(shí)現(xiàn)H5表單驗(yàn)證組件的示例
本文主要介紹了vue3實(shí)現(xiàn)H5表單驗(yàn)證組件的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04