一、vue中的響應式屬性
Vue中的數(shù)據(jù)實現(xiàn)響應式綁定
1、對象實現(xiàn)響應式:
是在初始化的時候利用definePrototype的定義set和get過濾器,在進行組件模板編譯時實現(xiàn)water的監(jiān)聽搜集依賴項,當數(shù)據(jù)發(fā)生變化時在set中通過調(diào)用dep.notify進行發(fā)布通知,實現(xiàn)視圖的更新。
2、數(shù)組實現(xiàn)響應式:
對于數(shù)組則是通過繼承重寫數(shù)組的方法splice、pop、push、shift、unshift、sort、reverse、等可以修改原數(shù)組的方式實現(xiàn)響應式的,但是通過length以及直接利用item[index]方式修改數(shù)組是不能實現(xiàn)響應式的改變dom(這種兩種方式涉及到數(shù)組的內(nèi)部實現(xiàn))。在數(shù)據(jù)更新后為了避免過于頻繁的進行dom的操作,在vue中會將更新的dom進行批量操作,而不是直接有數(shù)據(jù)更新就刷新dom,vue將需要更新的dom壓入異步隊列記性批量操作,提高性能。
下面具體的實現(xiàn),實現(xiàn)原理大致如下:
對象中實現(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到主題對象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é)點劫持到文檔片段中 } return flag; } function compile (node, vm) { var reg = /\{\{(.*)\}\}/; // 節(jié)點類型為元素 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) { // 給相應的data屬性賦值,進而觸發(fā)該屬性的set方法 vm[name] = e.target.value; }); node.value = vm[name]; // 將data的值賦給該node node.removeAttribute('v-model'); } }; new Watcher(vm, node, name, 'input'); } // 節(jié)點類型為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ā)相應屬性的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>
名稱欄目:Vue實現(xiàn)雙向綁定的原理以及響應式數(shù)據(jù)的方法-創(chuàng)新互聯(lián)
分享地址:http://www.ekvhdxd.cn/article0/cdcioo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、域名注冊、面包屑導航、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、云服務器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容