Vue實(shí)現(xiàn)雙向綁定的原理以及響應(yīng)式數(shù)據(jù)的方法-創(chuàng)新互聯(lián)

一、vue中的響應(yīng)式屬性

成都創(chuàng)新互聯(lián)從2013年創(chuàng)立,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元福綿做網(wǎng)站,已為上家服務(wù),為福綿各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話(huà):18982081108

Vue中的數(shù)據(jù)實(shí)現(xiàn)響應(yīng)式綁定

1、對(duì)象實(shí)現(xiàn)響應(yīng)式:

是在初始化的時(shí)候利用definePrototype的定義set和get過(guò)濾器,在進(jìn)行組件模板編譯時(shí)實(shí)現(xiàn)water的監(jiān)聽(tīng)搜集依賴(lài)項(xiàng),當(dāng)數(shù)據(jù)發(fā)生變化時(shí)在set中通過(guò)調(diào)用dep.notify進(jìn)行發(fā)布通知,實(shí)現(xiàn)視圖的更新。

2、數(shù)組實(shí)現(xiàn)響應(yīng)式:

對(duì)于數(shù)組則是通過(guò)繼承重寫(xiě)數(shù)組的方法splice、pop、push、shift、unshift、sort、reverse、等可以修改原數(shù)組的方式實(shí)現(xiàn)響應(yīng)式的,但是通過(guò)length以及直接利用item[index]方式修改數(shù)組是不能實(shí)現(xiàn)響應(yīng)式的改變dom(這種兩種方式涉及到數(shù)組的內(nèi)部實(shí)現(xiàn))。在數(shù)據(jù)更新后為了避免過(guò)于頻繁的進(jìn)行dom的操作,在vue中會(huì)將更新的dom進(jìn)行批量操作,而不是直接有數(shù)據(jù)更新就刷新dom,vue將需要更新的dom壓入異步隊(duì)列記性批量操作,提高性能。

下面具體的實(shí)現(xiàn),實(shí)現(xiàn)原理大致如下:

  

對(duì)象中實(shí)現(xiàn)雙向數(shù)據(jù)綁定,可以直接在瀏覽器查看效果:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Two-way data-binding</title>
</head>
<body>
 
 <div id="app">
  <input type="text" v-model="text">
  {{ text }}
 </div>

 <script>
  function observe (obj, vm) {
   Object.keys(obj).forEach(function (key) {
    defineReactive(vm, key, obj[key]);
   });
  }

  function defineReactive (obj, key, val) {

   var dep = new Dep();

   Object.defineProperty(obj, key, {
    get: function () {
     // 添加訂閱者watcher到主題對(duì)象Dep
     if (Dep.target) dep.addSub(Dep.target);
     return val
    },
    set: function (newVal) {
     if (newVal === val) return
     val = newVal;
     // 作為發(fā)布者發(fā)出通知
     dep.notify();
    }
   });
  }

  function nodeToFragment (node, vm) {
   var flag = document.createDocumentFragment();
   var child;
   
   while (child = node.firstChild) {
    compile(child, vm);
    flag.appendChild(child); // 將子節(jié)點(diǎn)劫持到文檔片段中
   }

   return flag;
  }

  function compile (node, vm) {
   var reg = /\{\{(.*)\}\}/;
   // 節(jié)點(diǎn)類(lèi)型為元素
   if (node.nodeType === 1) {
    var attr = node.attributes;
    // 解析屬性
    for (var i = 0; i < attr.length; i++) {
     if (attr[i].nodeName == 'v-model') {
      var name = attr[i].nodeValue; // 獲取v-model綁定的屬性名
      node.addEventListener('input', function (e) {
       // 給相應(yīng)的data屬性賦值,進(jìn)而觸發(fā)該屬性的set方法
       vm[name] = e.target.value;
      });
      node.value = vm[name]; // 將data的值賦給該node
      node.removeAttribute('v-model');
     }
    };

    new Watcher(vm, node, name, 'input');
   }
   // 節(jié)點(diǎn)類(lèi)型為text
   if (node.nodeType === 3) {
    if (reg.test(node.nodeValue)) {
     var name = RegExp.$1; // 獲取匹配到的字符串
     name = name.trim();

     new Watcher(vm, node, name, 'text');
    }
   }
  }

  function Watcher (vm, node, name, nodeType) {
   Dep.target = this;
   this.name = name;
   this.node = node;
   this.vm = vm;
   this.nodeType = nodeType;
   this.update();
   Dep.target = null;
  }

  Watcher.prototype = {
   update: function () {
    this.get();
    if (this.nodeType == 'text') {
     this.node.nodeValue = this.value;
    }
    if (this.nodeType == 'input') {
     this.node.value = this.value;
    }
   },
   // 獲取data中的屬性值
   get: function () {
    this.value = this.vm[this.name]; // 觸發(fā)相應(yīng)屬性的get
   }
  }

  function Dep () {
   this.subs = []
  }

  Dep.prototype = {
   addSub: function(sub) {
    this.subs.push(sub);
   },

   notify: function() {
    this.subs.forEach(function(sub) {
     sub.update();
    });
   }
  };

  function Vue (options) {
   this.data = options.data;
   var data = this.data;

   observe(data, this);

   var id = options.el;
   var dom = nodeToFragment(document.getElementById(id), this);

   // 編譯完成后,將dom返回到app中
   document.getElementById(id).appendChild(dom); 
  }

  var vm = new Vue({
   el: 'app',
   data: {
    text: 'hello world'
   }
  });

 </script>

</body>
</html>

名稱(chēng)欄目:Vue實(shí)現(xiàn)雙向綁定的原理以及響應(yīng)式數(shù)據(jù)的方法-創(chuàng)新互聯(lián)
分享地址:http://muchs.cn/article0/cdcioo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、域名注冊(cè)、面包屑導(dǎo)航、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)云服務(wù)器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)