Vue.js 中的 $watch使用方法
這兩天學(xué)習(xí)了Vue.js 中的 $watch這個地方知識點(diǎn)挺多的,而且很重要,所以,今天添加一點(diǎn)小筆記。
Observer, Watcher, vm 可謂 Vue 中比較重要的部分,檢測數(shù)據(jù)變動后視圖更新的重要環(huán)節(jié)。下面我們來看看 如何實現(xiàn)一個簡單的 $watch 功能,當(dāng)然Vue 中使用了很多優(yōu)化手段,在本文中暫不一一討論。
例子:
// 創(chuàng)建 vm let vm = new Vue({ data: 'a' }) // 鍵路徑 vm.$watch('a.b.c', function () { // 做點(diǎn)什么 })
先闡明在這個 demo 以及Vue 中,它們的關(guān)系:
vm 調(diào)用 $watch 后,首先調(diào)用 observe 函數(shù) 創(chuàng)建 Observer 實例觀察數(shù)據(jù),Observer 又創(chuàng)建 Dep , Dep 用來維護(hù)訂閱者。然后創(chuàng)建 Watcher 實例提供 update 函數(shù)。一旦數(shù)據(jù)變動,就層層執(zhí)行回調(diào)函數(shù)。
Observer和observe
遞歸調(diào)用 observe 函數(shù)創(chuàng)建 Observer。在創(chuàng)建 Observer 的過程中,使用 Object.defineProperty() 函數(shù)為其添加 get set 函數(shù), 并創(chuàng)建 Dep 實例。
export function observe (val) { if (!val || typeof val !== 'object') { return } return new Observer(val) }
function defineReactive (obj, key, val) { var dep = new Dep() var property = Object.getOwnPropertyDescriptor(obj, key) // 是否允許修改 if (property && property.configurable === false) { return } // 獲取定義好的 get set 函數(shù) var getter = property && property.get var setter = property && property.set var childOb = observe(val) Object.defineProperty(obj, key, { enumerable: true, configurable: true, get: () => { var value = getter ? getter.call(obj) : val // 說明是 Watcher 初始化時獲取的, 就添加訂閱者 if (Dep.target) { dep.depend() if (childOb) { childOb.dep.depend() } // if isArray do some.... } return value }, set: (newVal) => { var value = getter ? getter.call(obj) : val if (value === newVal) { return } if (setter) { setter.call(obj, newVal) } else { val = newVal } childOb = observe(newVal) dep.notify() } }) }
你可能會疑問 Dep.target 是個什么鬼?😳
答案是:Watcher, 我們接下來看
Dep
export default function Dep () { this.subs = [] } // 就是你??!~ Dep.target = null // 添加訂閱者 Dep.prototype.addSub = function (sub) { this.subs.push(sub) } // 添加依賴 Dep.prototype.depend = function () { Dep.target.addDep(this) } // 通知訂閱者:要更新啦~ Dep.prototype.notify = function () { this.subs.forEach(sub => sub.update()) }
Watcher
為了給每個數(shù)據(jù)添加訂閱者,我們想到的辦法是在數(shù)據(jù)的 get 函數(shù)中, 但是 get 函數(shù)會調(diào)用很多次呀~。。。 腫么辦?那就給 Dep 添加個參數(shù) target
export default function Watcher (vm, expOrFn, cb) { this.cb = cb this.vm = vm this.expOrFn = expOrFn this.value = this.get() } Watcher.prototype.get = function () { Dep.target = this const value = this.vm._data[this.expOrFn] // 此時 target 有值,此時執(zhí)行到了上面的 defineReactive 函數(shù)中 get 函數(shù)。就添加訂閱者 Dep.target = null // 為了不重復(fù)添加 就設(shè)置為 null return value }
Vue Instance
在 Vue Instance 做得最多的事情就是初始化 State, 添加函數(shù)等等。
// Vue 實例 export default function Vue(options) { this.$options = options this._initState() } // 初始化State Vue.prototype._initState = function () { let data = this._data = this.$options.data Object.keys(data).forEach(key => this._proxy(key)) observe(data, this) } // $watch 函數(shù), Vue.prototype.$watch = function (expOrFn, fn, options) { new Watcher(this, expOrFn, fn) }
總結(jié)
至此,我們已經(jīng)實現(xiàn)了一個簡單的 $watch 函數(shù), Object.defineProperty() 函數(shù)可謂是舉足輕重, 因此不支持該函數(shù)的瀏覽器, Vue 均不支持。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue3利用v-model實現(xiàn)父子組件之間數(shù)據(jù)同步的代碼詳解
在Vue 3中,v-model這一指令也得到了升級,使得父子組件之間的數(shù)據(jù)同步變得更加容易和靈活,本文將探討一下Vue 3中如何利用v-model來實現(xiàn)父子組件之間的數(shù)據(jù)同步,需要的朋友可以參考下2024-03-03Vue+ElementUI實現(xiàn)從后臺動態(tài)填充下拉框的示例代碼
本文主要介紹了Vue+ElementUI實現(xiàn)從后臺動態(tài)填充下拉框的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02Vue3+element實現(xiàn)表格數(shù)據(jù)導(dǎo)出
這篇文章主要為大家學(xué)習(xí)介紹了Vue3如何利用element實現(xiàn)表格數(shù)據(jù)導(dǎo)出功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴快跟隨小編一起學(xué)習(xí)一下吧2023-07-07element-ui之關(guān)于組件BackToTop回到頂部的使用
這篇文章主要介紹了element-ui之關(guān)于組件BackToTop回到頂部的使用,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03