Vue.js中數(shù)組變動(dòng)的檢測(cè)詳解
前言
最近在嘗試用Vue.js重構(gòu)公司的現(xiàn)有業(yè)務(wù)代碼,組件化的設(shè)計(jì)思路和MVVM的思想讓我深深沉迷于其中。但是是踩到了不少坑,就比如這篇文章介紹的數(shù)組綁定后的更新檢測(cè)。
相信大家都知道Observer
,Watcher
,vm
可謂 Vue 中比較重要的部分,檢測(cè)數(shù)據(jù)變動(dòng)后視圖更新的重要環(huán)節(jié)。在 vue.js中$watch的用法示例 中,我們討論了如何實(shí)現(xiàn)基本的 watch 。
接下來,我們來看看如何實(shí)現(xiàn)數(shù)組變動(dòng)檢測(cè)。
例子:
// 創(chuàng)建 vm let vm = new Vue({ data: { a: [{}, {}, {}] } }) // 鍵路徑 vm.$watch('a', function () { // 做點(diǎn)什么 })
思路
在 js 中, 很容易實(shí)現(xiàn) hook, 比如:
// hook 一個(gè) console。log let _log = console.log console.log = function (data) { // do someting _log.call(this, data) }
我們只要實(shí)現(xiàn)自定義的函數(shù),就能觀測(cè)到數(shù)組變動(dòng)。
Vue.js 中使用Object.create()
這個(gè)函數(shù)來實(shí)現(xiàn)繼承, 從而實(shí)現(xiàn)自定義函數(shù),以觀測(cè)數(shù)組。
// 簡單介紹 var a = new Object(); // 創(chuàng)建一個(gè)對(duì)象,沒有父類 var b = Object.create(a.prototype); // b 繼承了a的原型
繼承
array.js定義如下:
// 獲取原型 const arrayProto = Array.prototype // 創(chuàng)建新原型對(duì)象 export const arrayMethods = Object.create(arrayProto) // 給新原型實(shí)現(xiàn)這些函數(shù) [ 'push', 'pop', 'shift', 'unshift', 'splice', 'sort', 'reverse' ] .forEach(function (method) { // 獲取新原型函數(shù) (此時(shí)未實(shí)現(xiàn) undefined) const original = arrayProto[method] // 給新原型添加函數(shù)實(shí)現(xiàn) Object.defineProperty(arrayMethods, method, { value: function mutator() { let i = arguments.length // 獲取參數(shù) const args = new Array(i) while (i--) { args[i] = arguments[i] } // 實(shí)現(xiàn)函數(shù) const result = original.apply(this, args) // 獲取觀察者 const ob = this.__ob__ // 是否更改數(shù)組本身 let inserted switch (method) { case 'push': inserted = args break case 'unshift': inserted = args break case 'splice': inserted = args.slice(2) break } // 觀察新數(shù)組 inserted && ob.observeArray(inserted) // 觸發(fā)更新 ob.dep.notify() return result }, enumerable: true, writable: true, configurable: true }) })
ok, 我們定義完了 array.js, 并作為模塊導(dǎo)出,修改 Observer
的實(shí)現(xiàn):
export function Observer (value) { this.dep = new Dep() this.value = value // 如果是數(shù)組就更改其原型指向 if (Array.isArray(value)) { value.__proto__ = arrayMethods this.observeArray(value) } else { this.walk(value) } } // 觀測(cè)數(shù)據(jù)元素 Observer.prototype.observeArray = function (items) { for (let i = 0, l = items.length; i < l; i++) { observe(items[i]) } }
Observer
修改完畢后,我們?cè)倏纯?Watcher
, 只需要改動(dòng)其 update
函數(shù),
Watcher.prototype.update = function (dep) { console.log('2.update') const value = this.get() const oldValue = this.value this.value = value if (value !== this.value || value !== null) { this.cb.call(this.vm, value, oldValue) // 如果沒有此函數(shù), 會(huì)導(dǎo)致重復(fù)調(diào)用 $watch 回調(diào)函數(shù)。 // 原因:數(shù)組變異過了,并對(duì)新數(shù)組也進(jìn)行了觀察,應(yīng)該移除舊的觀察者 dep.subs.shift() } }
結(jié)果:
const vm = new Vue({ data: { b: [{a: 'a'}, {b: 'b'}] } }) vm.$watch('b', (val) => { console.log('------我看到你們了-----') }) vm.b.push({c: 'c'}) vm.b.pop({c: 'c'}) vm.b.push({c: 'c'}) // 結(jié)果: // console.log('------我看到你們了-----') // console.log('------我看到你們了-----') // console.log('------我看到你們了-----')
總結(jié)
至此,我們已經(jīng)實(shí)現(xiàn)對(duì)數(shù)組變動(dòng)的檢測(cè)。主要使用了Object.create()函數(shù)。希望這篇文章的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
詳解Vue-Cli 異步加載數(shù)據(jù)的一些注意點(diǎn)
本篇文章主要介紹了詳解Vue-Cli 異步加載數(shù)據(jù)的一些注意點(diǎn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08vue中組件的過渡動(dòng)畫及實(shí)現(xiàn)代碼
這篇文章主要介紹了vue中組件的過渡動(dòng)畫,并通過實(shí)例代碼給大家介紹了過渡動(dòng)畫的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11vue按需加載組件webpack require.ensure的方法
本篇文章主要介紹了vue按需加載組件webpack require.ensure的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12element-vue實(shí)現(xiàn)網(wǎng)頁鎖屏功能(示例代碼)
這篇文章主要介紹了element-vue實(shí)現(xiàn)網(wǎng)頁鎖屏功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-11-11vue2+elementui上傳照片方式(el-upload超簡單)
這篇文章主要介紹了vue2+elementui上傳照片方式(el-upload超簡單),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03VUE多個(gè)下拉框?qū)崿F(xiàn)雙向聯(lián)動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了VUE多個(gè)下拉框?qū)崿F(xiàn)雙向聯(lián)動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07Vue中引用assets中圖片的實(shí)現(xiàn)方式
這篇文章主要介紹了Vue中引用assets中圖片的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10