泉州網(wǎng)站建設(shè)方案策劃東莞疫情最新消息今天
一、什么是雙向綁定
我們先從單向綁定切入單向綁定非常簡單,就是把Model
綁定到View
,當我們用JavaScript
代碼更新Model
時,View
就會自動更新雙向綁定就很容易聯(lián)想到了,在單向綁定的基礎(chǔ)上,用戶更新了View
,Model
的數(shù)據(jù)也自動被更新了,這種情況就是雙向綁定舉個栗子
當用戶填寫表單時,View
的狀態(tài)就被更新了,如果此時可以自動更新Model
的狀態(tài),那就相當于我們把Model
和View
做了雙向綁定關(guān)系圖如下
二、雙向綁定的原理是什么
我們都知道?Vue
?是數(shù)據(jù)雙向綁定的框架,雙向綁定由三個重要部分構(gòu)成
- 數(shù)據(jù)層(Model):應用的數(shù)據(jù)及業(yè)務(wù)邏輯
- 視圖層(View):應用的展示效果,各類UI組件
- 業(yè)務(wù)邏輯層(ViewModel):框架封裝的核心,它負責將數(shù)據(jù)與視圖關(guān)聯(lián)起來
而上面的這個分層的架構(gòu)方案,可以用一個專業(yè)術(shù)語進行稱呼:MVVM
這里的控制層的核心功能便是 “數(shù)據(jù)雙向綁定” 。自然,我們只需弄懂它是什么,便可以進一步了解數(shù)據(jù)綁定的原理
理解ViewModel
它的主要職責就是:
- 數(shù)據(jù)變化后更新視圖
- 視圖變化后更新數(shù)據(jù)
當然,它還有兩個主要部分組成
- 監(jiān)聽器(Observer):對所有數(shù)據(jù)的屬性進行監(jiān)聽
- 解析器(Compiler):對每個元素節(jié)點的指令進行掃描跟解析,根據(jù)指令模板替換數(shù)據(jù),以及綁定相應的更新函數(shù)
三、實現(xiàn)雙向綁定
我們還是以Vue
為例,先來看看Vue
中的雙向綁定流程是什么的
new Vue()
首先執(zhí)行初始化,對data
執(zhí)行響應化處理,這個過程發(fā)生Observe
中- 同時對模板執(zhí)行編譯,找到其中動態(tài)綁定的數(shù)據(jù),從
data
中獲取并初始化視圖,這個過程發(fā)生在Compile
中 - 同時定義?個更新函數(shù)和
Watcher
,將來對應數(shù)據(jù)變化時Watcher
會調(diào)用更新函數(shù) - 由于
data
的某個key
在?個視圖中可能出現(xiàn)多次,所以每個key
都需要?個管家Dep
來管理多個Watcher
- 將來data中數(shù)據(jù)?旦發(fā)生變化,會首先找到對應的
Dep
,通知所有Watcher
執(zhí)行更新函數(shù)
流程圖如下:
實現(xiàn)
先來一個構(gòu)造函數(shù):執(zhí)行初始化,對data
執(zhí)行響應化處理
class Vue { constructor(options) { this.$options = options; this.$data = options.data; // 對data選項做響應式處理 observe(this.$data); // 代理data到vm上 proxy(this); // 執(zhí)行編譯 new Compile(options.el, this); }
}
對data
選項執(zhí)行響應化具體操作
function observe(obj) { if (typeof obj !== "object" || obj == null) { return; } new Observer(obj);
} class Observer { constructor(value) { this.value = value; this.walk(value); } walk(obj) { Object.keys(obj).forEach((key) => { defineReactive(obj, key, obj[key]); }); }
}
編譯Compile
對每個元素節(jié)點的指令進行掃描跟解析,根據(jù)指令模板替換數(shù)據(jù),以及綁定相應的更新函數(shù)
class Compile { constructor(el, vm) { this.$vm = vm; this.$el = document.querySelector(el); // 獲取dom if (this.$el) { this.compile(this.$el); } } compile(el) { const childNodes = el.childNodes; Array.from(childNodes).forEach((node) => { // 遍歷子元素 if (this.isElement(node)) { // 判斷是否為節(jié)點 console.log("編譯元素" + node.nodeName); } else if (this.isInterpolation(node)) { console.log("編譯插值?本" + node.textContent); // 判斷是否為插值文本 {{}} } if (node.childNodes && node.childNodes.length > 0) { // 判斷是否有子元素 this.compile(node); // 對子元素進行遞歸遍歷 } }); } isElement(node) { return node.nodeType == 1; } isInterpolation(node) { return node.nodeType == 3 && /\{\{(.*)\}\}/.test(node.textContent); }
}
依賴收集
視圖中會用到data
中某key
,這稱為依賴。同?個key
可能出現(xiàn)多次,每次都需要收集出來用?個Watcher
來維護它們,此過程稱為依賴收集多個Watcher
需要?個Dep
來管理,需要更新時由Dep
統(tǒng)?通知
實現(xiàn)思路
defineReactive
時為每?個key
創(chuàng)建?個Dep
實例- 初始化視圖時讀取某個
key
,例如name1
,創(chuàng)建?個watcher1
- 由于觸發(fā)
name1
的getter
方法,便將watcher1
添加到name1
對應的Dep中 - 當
name1
更新,setter
觸發(fā)時,便可通過對應Dep
通知其管理所有Watcher
更新
// 負責更新視圖
class Watcher { constructor(vm, key, updater) { this.vm = vm this.key = key this.updaterFn = updater // 創(chuàng)建實例時,把當前實例指定到Dep.target靜態(tài)屬性上 Dep.target = this // 讀一下key,觸發(fā)get vm[key] // 置空 Dep.target = null } // 未來執(zhí)行dom更新函數(shù),由dep調(diào)用的 update() { this.updaterFn.call(this.vm, this.vm[this.key]) }
}
聲明Dep
class Dep { constructor() { this.deps = []; // 依賴管理 } addDep(dep) { this.deps.push(dep); } notify() { this.deps.forEach((dep) => dep.update()); }
}
創(chuàng)建watcher
時觸發(fā)getter
class Watcher { constructor(vm, key, updateFn) { Dep.target = this; this.vm[this.key]; Dep.target = null; }
}
依賴收集,創(chuàng)建Dep
實例
function defineReactive(obj, key, val) { this.observe(val); const dep = new Dep(); Object.defineProperty(obj, key, { get() { Dep.target && dep.addDep(Dep.target);// Dep.target也就是Watcher實例 return val; }, set(newVal) { if (newVal === val) return; dep.notify(); // 通知dep執(zhí)行更新方法 }, });
}
參考文獻
- https://www.liaoxuefeng.com/wiki/1022910821149312/1109527162256416
- https://juejin.cn/post/6844903942254510087#heading-9