当前位置: 移动技术网 > IT编程>脚本编程>vue.js > Vue Object.defineProperty及ProxyVue实现双向数据绑定

Vue Object.defineProperty及ProxyVue实现双向数据绑定

2020年09月03日  | 移动技术网IT编程  | 我要评论
双向数据绑定无非就是,视图 => 数据,数据 => 视图的更新过程以下的方案中的实现思路: 定义一个vue的构造函数并初始化这个函数(myvue.prototype._init) 实现

双向数据绑定无非就是,视图 => 数据,数据 => 视图的更新过程

以下的方案中的实现思路:

  • 定义一个vue的构造函数并初始化这个函数(myvue.prototype._init)
  • 实现数据层的更新:数据劫持,定义一个 obverse 函数重写data的set和get(myvue.prototype._obsever)
  • 实现视图层的更新:订阅者模式,定义个 watcher 函数实现对dom的更新(watcher)
  • 将数据和视图层进行绑定,解析指令v-bind、v-model、v-click(myvue.prototype._compile)
  • 创建vue实例(new myvue)

1.object.defineproperty方式实现双向数据绑定

<!doctype html>
<html>
 
<head>
 <title>myvue</title>
 <style>
  #app{
  text-align: center;
 }
</style>
</head>
 
<body>
 <div id="app">
  <form>
   <input type="text" v-model="number" />
   <button type="button" v-click="increment">增加</button>
  </form>
  <h3 v-bind="number"></h3>
 </div>
</body>
<script>
 
 // 定义一个myvue构造函数
 function myvue(option) {
  this._init(option)
 }
 
 myvue.prototype._init = function (options) { // 传了一个配置对象
  this.$options = options // options 为上面使用时传入的结构体,包括el,data,methods
  this.$el = document.queryselector(options.el) // el是 #app, this.$el是id为app的element元素
  this.$data = options.data // this.$data = {number: 0}
  this.$methods = options.methods // this.$methods = {increment: function(){}}
 
 
  // _binding保存着model与view的映射关系,也就是我们前面定义的watcher的实例。当model改变时,我们会触发其中的指令类更新,保证view也能实时更新
  this._binding = {}
 
  this._obsever(this.$data)
  this._compile(this.$el)
 }
 
 // 数据劫持:更新数据
 myvue.prototype._obsever = function (obj) {
  let _this = this
  object.keys(obj).foreach((key) => { // 遍历obj对象
   if (obj.hasownproperty(key)) { // 判断 obj 对象是否包含 key属性
    _this._binding[key] = [] // 按照前面的数据,_binding = {number: []} 存储 每一个 new watcher
   }
   let value = obj[key]
   if (typeof value === 'object') { //如果值还是对象,则遍历处理
    _this._obsever(value)
   }
   object.defineproperty(_this.$data, key, {
    enumerable: true,
    configurable: true,
    get: () => { // 获取 value 值
     return value
    },
    set: (newval) => { // 更新 value 值
     if (value !== newval) {
      value = newval
      _this._binding[key].foreach((item) => { // 当number改变时,触发_binding[number] 中的绑定的watcher类的更新
       item.update() // 调 watcher 实例的 update 方法更新 dom
      })
     }
    }
   })
  })
 }
 
 // 订阅者模式: 绑定更新函数,实现对 dom 元素的更新
 function watcher(el, data, key, attr) {
  this.el = el // 指令对应的dom元素
  this.data = data // this.$data 数据: {number: 0, count: 0}
  this.key = key // 指令绑定的值,本例如"number"
  this.attr = attr // 绑定的属性值,本例为"innerhtml","value"
 
  this.update()
 }
 // 比如 h3.innerhtml = this.data.number; 当number改变时,会触发这个update函数,保证对应的dom内容进行了更新
 watcher.prototype.update = function () {
  this.el[this.attr] = this.data[this.key]
 }
 
 // 将view与model进行绑定,解析指令(v-bind,v-model,v-clickde)等
 myvue.prototype._compile = function (el) { // root 为id为app的element元素,也就是我们的根元素
  let _this = this
  let nodes = array.prototype.slice.call(el.children) // 将为数组转化为真正的数组
  nodes.map(node => {
   if (node.children.length && node.children.length > 0) { // 对所有元素进行遍历,并进行处理
    _this._compile(node)
   }
   if (node.hasattribute('v-click')) { // 如果有v-click属性,我们监听它的onclick事件,触发increment事件,即number++
    let attrval = node.getattribute('v-click')
    node.onclick = _this.$methods[attrval].bind(_this.$data) // bind是使data的作用域与method函数的作用域保持一致
   }
 
   // 如果有v-model属性,并且元素是input或者textarea,我们监听它的input事件
   if (node.hasattribute('v-model') && (node.tagname === 'input' || node.tagname === 'textarea')) {
    let attrval = node.getattribute('v-model')
 
    _this._binding[attrval].push(new watcher(
     node, // 对应的 dom 节点
     _this.$data,
     attrval, // v-model 绑定的值
     'value'
    ))
    node.addeventlistener('input', () => {
     _this.$data[attrval] = node.value // 使number 的值与 node的value保持一致,已经实现了双向绑定
    })
   }
   if (node.hasattribute('v-bind')) {
    let attrval = node.getattribute('v-bind')
    _this._binding[attrval].push(new watcher(
     node,
     _this.$data,
     attrval, // v-bind 绑定的值
     'innerhtml'
    ))
   }
  })
 }
 
 
 window.onload = () => { // 当文档内容完全加载完成会触发该事件,避免获取不到对象的情况
  new myvue({
   el: '#app',
   data: {
    number: 0,
    count: 0
   },
   methods: {
    increment() {
     this.number++
    },
    incre() {
     this.count++
    }
   }
  })
 }
</script>
 
</html>

2.proxy 实现双向数据绑定

<!doctype html>
<html>
 
<head>
 <title>myvue</title>
 <style>
  #app{
  text-align: center;
 }
</style>
</head>
 
<body>
 <div id="app">
  <form>
   <input type="text" v-model="number" />
   <button type="button" v-click="increment">增加</button>
  </form>
  <h3 v-bind="number"></h3>
 </div>
</body>
<script>
 
 // 定义一个myvue构造函数
 function myvue(option) {
  this._init(option)
 }
 
 myvue.prototype._init = function (options) { // 传了一个配置对象
  this.$options = options // options 为上面使用时传入的结构体,包括el,data,methods
  this.$el = document.queryselector(options.el) // el是 #app, this.$el是id为app的element元素
  this.$data = options.data // this.$data = {number: 0}
  this.$methods = options.methods // this.$methods = {increment: function(){}}
 
  this._binding = {}
  this._obsever(this.$data)
  this._complie(this.$el)
 
 }
 
// 数据劫持:更新数据
myvue.prototype._obsever = function (data) {
  let _this = this
  let handler = {
   get(target, key) {
    return target[key]; // 获取该对象上key的值
   },
   set(target, key, newvalue) {
    let res = reflect.set(target, key, newvalue); // 将新值分配给属性的函数
    _this._binding[key].map(item => {
     item.update();
    });
    return res;
   }
  };
  // 把代理器返回的对象代理到this.$data,即this.$data是代理后的对象,外部每次对this.$data进行操作时,实际上执行的是这段代码里handler对象上的方法
  this.$data = new proxy(data, handler);
 }
 
 // 将view与model进行绑定,解析指令(v-bind,v-model,v-clickde)等
 myvue.prototype._complie = function (el) { // el 为id为app的element元素,也就是我们的根元素
  let _this = this
  let nodes = array.prototype.slice.call(el.children) // 将为数组转化为真正的数组
 
  nodes.map(node => {
   if (node.children.length && node.children.length > 0) this._complie(node)
   if (node.hasattribute('v-click')) { // 如果有v-click属性,我们监听它的onclick事件,触发increment事件,即number++
    let attrval = node.getattribute('v-click')
    node.onclick = _this.$methods[attrval].bind(_this.$data) // bind是使data的作用域与method函数的作用域保持一致
   }
 
   // 如果有v-model属性,并且元素是input或者textarea,我们监听它的input事件
   if (node.hasattribute('v-model') && (node.tagname === 'input' || node.tagname === 'textarea')) {
    let attrval = node.getattribute('v-model')
    
    console.log(_this._binding)
    if (!_this._binding[attrval]) _this._binding[attrval] = []
    _this._binding[attrval].push(new watcher(
     node, // 对应的 dom 节点
     _this.$data,
     attrval, // v-model 绑定的值
     'value',
    ))
    node.addeventlistener('input', () => {
     _this.$data[attrval] = node.value // 使number 的值与 node的value保持一致,已经实现了双向绑定
    })
   }
   if (node.hasattribute('v-bind')) {
    let attrval = node.getattribute('v-bind')
    if (!_this._binding[attrval]) _this._binding[attrval] = []
    _this._binding[attrval].push(new watcher(
     node,
     _this.$data,
     attrval, // v-bind 绑定的值
     'innerhtml',
    ))
   }
 
  })
 }
 // 绑定更新函数,实现对 dom 元素的更新
 function watcher(el, data, key, attr) {
  this.el = el // 指令对应的dom元素
  this.data = data // 代理的对象 this.$data 数据: {number: 0, count: 0}
  this.key = key // 指令绑定的值,本例如"num"
  this.attr = attr // 绑定的属性值,本例为"innerhtml","value"
 
  this.update()
 }
 // 比如 h3.innerhtml = this.data.number; 当number改变时,会触发这个update函数,保证对应的dom内容进行了更新
 watcher.prototype.update = function () {
  this.el[this.attr] = this.data[this.key]
 }
 
 window.onload = () => { // 当文档内容完全加载完成会触发该事件,避免获取不到对象的情况
  new myvue({
   el: '#app',
   data: {
    number: 0,
    count: 0
   },
   methods: {
    increment() {
     this.number++
    },
    incre() {
     this.count++
    }
   }
  })
 }
</script>
 
</html>

3.将上面代码改成class的写法

<!doctype html>
<html>
 
<head>
 <title>myvue</title>
 <style>
  #app{
  text-align: center;
 }
</style>
</head>
 
<body>
 <div id="app">
  <form>
   <input type="text" v-model="number" />
   <button type="button" v-click="increment">增加</button>
  </form>
  <h3 v-bind="number"></h3>
 </div>
</body>
<script>
 
 class myvue {
  constructor(options) { // 接收了一个配置对象
   this.$options = options // options 为上面使用时传入的结构体,包括el,data,methods
   this.$el = document.queryselector(options.el) // el是 #app, this.$el是id为app的element元素
   this.$data = options.data // this.$data = {number: 0}
   this.$methods = options.methods // this.$methods = {increment: function(){}}
 
   this._binding = {}
   this._obsever(this.$data)
   this._complie(this.$el)
  }
  _obsever (data) { // 数据劫持:更新数据
   let _this = this
   let handler = {
    get(target, key) {
     return target[key]; // 获取该对象上key的值
    },
    set(target, key, newvalue) {
     let res = reflect.set(target, key, newvalue); // 将新值分配给属性的函数
     _this._binding[key].map(item => {
      item.update();
     });
     return res;
    }
   };
   // 把代理器返回的对象代理到this.$data,即this.$data是代理后的对象,外部每次对this.$data进行操作时,实际上执行的是这段代码里handler对象上的方法
   this.$data = new proxy(data, handler);
  }
  _complie(el) { // el 为id为app的element元素,也就是我们的根元素
   let _this = this
   let nodes = array.prototype.slice.call(el.children) // 将为数组转化为真正的数组
 
   nodes.map(node => {
    if (node.children.length && node.children.length > 0) this._complie(node)
    if (node.hasattribute('v-click')) { // 如果有v-click属性,我们监听它的onclick事件,触发increment事件,即number++
     let attrval = node.getattribute('v-click')
     node.onclick = _this.$methods[attrval].bind(_this.$data) // bind是使data的作用域与method函数的作用域保持一致
    }
 
    // 如果有v-model属性,并且元素是input或者textarea,我们监听它的input事件
    if (node.hasattribute('v-model') && (node.tagname === 'input' || node.tagname === 'textarea')) {
     let attrval = node.getattribute('v-model')
     if (!_this._binding[attrval]) _this._binding[attrval] = []
     _this._binding[attrval].push(new watcher(
      node, // 对应的 dom 节点
      _this.$data,
      attrval, // v-model 绑定的值
      'value',
     ))
     node.addeventlistener('input', () => {
      _this.$data[attrval] = node.value // 使number 的值与 node的value保持一致,已经实现了双向绑定
     })
    }
    if (node.hasattribute('v-bind')) {
     let attrval = node.getattribute('v-bind')
     if (!_this._binding[attrval]) _this._binding[attrval] = []
     _this._binding[attrval].push(new watcher(
      node,
      _this.$data,
      attrval, // v-bind 绑定的值
      'innerhtml',
     ))
    }
 
   })
  }
 }
 
 class watcher {
  constructor (el, data, key, attr) {
   this.el = el // 指令对应的dom元素
   this.data = data // 代理的对象 this.$data 数据: {number: 0, count: 0}
   this.key = key // 指令绑定的值,本例如"num"
   this.attr = attr // 绑定的属性值,本例为"innerhtml","value"
   this.update()
  }
 
  update () {
   this.el[this.attr] = this.data[this.key]
  }
 }
 
 
 
 window.onload = () => { // 当文档内容完全加载完成会触发该事件,避免获取不到对象的情况
  new myvue({
   el: '#app',
   data: {
    number: 0,
    count: 0
   },
   methods: {
    increment() {
     this.number++
    },
    incre() {
     this.count++
    }
   }
  })
 }
</script>
 
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网