Vue数据双向绑定(即数据响应式),是利用了Object.defineProperty()这个方法从新定义了对象获取属性值get和设置属性值set的操做来实现的。html
var obj = {};
Object.defineProperty(obj, 'name', {
get: function() {
console.log('我被获取了')
return val;
},
set: function (newVal) {
console.log('我被设置了')
}
})
obj.name = 'zhangsan';//在给obj设置name属性的时候,触发了set这个方法
var val = obj.name;//在获得obj的name属性,会触发get方法
复制代码
Vue3.0版本中是采用ES6的Proxy对象来实现数据双向绑定的。vue
咱们先大体看下vue编译解析的总体架构图,以下图所示: node
<div id="app">
<h3>数据双向绑定</h3>
<div>
<div v-text="myText"></div>
<input type="text" v-model="myText"/>
</div>
</div>
复制代码
// 发布者
class Vue{
constructor(options) {
this.options = options;
this.$data = options.data;
this.$el = document.querySelector(options.el);// #app 获取app对象
this._directive = {};// 容器:存放订阅者
this.Observe(this.$data);
this.Compile(this.$el);
}
// 劫持数据
Observe(data) {}
// 解析指令
Compile(el) {}
}
// 订阅者
class Watcher {
}
// 实例化Vue对象
var app = new Vue({
el: '#app',
data: {
myText: 'hello world!'
}
})
复制代码
// 发布者
class Vue{
constructor(options) {
this.options = options;
this.$data = options.data;
this.$el = document.querySelector(options.el);// #app 获取app对象
this._directive = {};// 容器:存放订阅者
this.Observe(this.$data);
this.Compile(this.$el);
}
// 劫持数据
Observe(data) {
for (let key in data) {
this._directive[key] = []
}
}
// 解析指令
Compile(el) {
let nodes = el.children;
for (let i = 0;i < nodes.length;i++) {
let node = nodes[i];
// 递归,把嵌套的元素都查找,看是否有指令
if(node.children.length) {
this.Compile(node)
}
if(node.hasAttribute('v-text')) {
let attrVal = node.getAttribute('v-text');
this._directive[attrVal].push(new Watcher(node, this, attrVal, 'innerHTML'));
}
if(node.hasAttribute('v-model')) {
let attrVal = node.getAttribute('v-model');
this._directive[attrVal].push(new Watcher(node, this, attrVal, 'value'));
}
}
}
}
// 订阅者
class Watcher {
constructor(el, vm, exp, attr) {
this.el = el;// 元素对象 div input
this.vm = vm;
this.exp = exp;
this.attr = attr;
this.update();
}
update() {
this.el[this.attr] = this.vm.$data[this.exp];
}
}
复制代码
// 发布者
class Vue{
constructor(options) {
this.options = options;
this.$data = options.data;
this.$el = document.querySelector(options.el);// #app 获取app对象
this._directive = {};// 容器:存放订阅者
this.Observe(this.$data);
this.Compile(this.$el);
}
// 劫持数据
Observe(data) {
for (let key in data) {
this._directive[key] = [];
let val = data[key];
let watch = this._directive[key];
Object.defineProperty(data, key, {
get: function() {
return val;
},
set: function(newVal) {
if(newVal !== val) {
val = newVal;
watch.forEach(element => {
element.update()
})
}
}
})
}
}
// 解析指令
Compile(el) {
let nodes = el.children;
for (let i = 0;i < nodes.length;i++) {
let node = nodes[i];
// 递归,把嵌套的元素都查找,看是否有指令
if(node.children.length) {
this.Compile(node)
}
if(node.hasAttribute('v-text')) {
let attrVal = node.getAttribute('v-text');
this._directive[attrVal].push(new Watcher(node, this, attrVal, 'innerHTML'));
}
if(node.hasAttribute('v-model')) {
let attrVal = node.getAttribute('v-model');
this._directive[attrVal].push(new Watcher(node, this, attrVal, 'value'));
node.addEventListener('input', () => {
this.$data[attrVal] = node.value;
})
}
}
}
}
// 订阅者
class Watcher {
constructor(el, vm, exp, attr) {
this.el = el;// 元素对象 div input
this.vm = vm;
this.exp = exp;
this.attr = attr;
this.update();
}
update() {
this.el[this.attr] = this.vm.$data[this.exp];
}
}
复制代码
经过js来实现数据双向绑定,咱们就更深刻的了解Vue2.0的数据双向绑定原理。但愿本篇博客能够给学习Vue的小伙伴带来一些帮助!之后有机会再对Vue3.0 Proxy对象进行分析~~bash