文章目录
一、双向数据绑定的概念
单向绑定与双向绑定的区别
单向绑定是将模型(Model)的数据绑定到视图(View)上,当模型数据发生变化时,视图会自动更新。而双向绑定在此基础上增加了视图到模型的反馈机制,即视图状态的变化也能自动反映到模型数据上。
双向绑定的实例
以表单输入为例,当用户在输入框中填写信息时,视图的状态发生变化,如果这种变化能实时更新到模型数据,那么就实现了双向绑定。
二、双向绑定的原理
MVVM架构
双向绑定是MVVM(Model-View-ViewModel)架构的核心特性之一。在MVVM中,ViewModel负责将模型和视图关联起来,实现数据的双向流动。
ViewModel的工作原理
ViewModel包含两个主要部分:
- 监听器(Observer):负责监听模型数据的变化。
- 解析器(Compiler):负责解析视图中的指令,并根据指令模板替换数据,同时绑定更新函数。
三、Vue中的双向绑定实现
双向绑定流程
在Vue中,双向绑定的流程包括以下几个步骤:
- 初始化Vue实例,对数据进行响应化处理。
- 编译模板,找到动态绑定的数据,并初始化视图。
- 定义更新函数和Watcher,用于数据变化时更新视图。
- 使用Dep管理多个Watcher,确保数据变化时能够通知所有相关的Watcher。
实现双向绑定的关键代码
以下是对Vue双向绑定关键部分的代码实现:
// Vue构造函数
class Vue {
constructor(options) {
this.$options = options;
this.$data = options.data;
observe(this.$data);
proxy(this);
new Compile(options.el, this);
}
}
// 数据响应化处理
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]);
});
}
}
// 编译器
class Compile {
constructor(el, vm) {
this.$vm = vm;
this.$el = document.querySelector(el);
if (this.$el) {
this.compile(this.$el);
}
}
compile(el) {
const childNodes = el.childNodes;
Array.from(childNodes).forEach((node) => {
if (this.isElement(node)) {
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);
}
}
// 依赖收集
class Watcher {
constructor(vm, key, updater) {
this.vm = vm;
this.key = key;
this.updaterFn = updater;
Dep.target = this;
vm[key];
Dep.target = null;
}
update() {
this.updaterFn.call(this.vm, this.vm[this.key]);
}
}
class Dep {
constructor() {
this.deps = [];
}
addDep(dep) {
this.deps.push(dep);
}
notify() {
this.deps.forEach((dep) => dep.update());
}
}
function defineReactive(obj, key, val) {
this.observe(val);
const dep = new Dep();
Object.defineProperty(obj, key, {
get() {
Dep.target && dep.addDep(Dep.target);
return val;
},
set(newVal) {
if (newVal === val) return;
dep.notify();
},
});
}