vue實(shí)現(xiàn)簡(jiǎn)單的MVVM框架
不知不覺(jué)接觸前端的時(shí)間已經(jīng)過(guò)去半年了,越來(lái)越發(fā)覺(jué)對(duì)知識(shí)的學(xué)習(xí)不應(yīng)該只停留在會(huì)用的層面,這在我學(xué)jQuery的一段時(shí)間后便有這樣的體會(huì)。
雖然jQuery只是一個(gè)JS的代碼庫(kù),只要會(huì)一些JS的基本操作學(xué)習(xí)一兩天就能很快掌握jQuery的基本語(yǔ)法并熟練使用,但是如果不了解jQUery庫(kù)背后的實(shí)現(xiàn)原理,相信只要你一段時(shí)間不再使用jQuery的話就會(huì)把jQuery忘得一干二凈,這也許就是知其然不知其所以然的后果。
最近在學(xué)vue的時(shí)候又再一次經(jīng)歷了這樣的困惑,雖然能夠比較熟練的掌握vue的基本使用,也能夠?qū)V*模式、數(shù)據(jù)劫持、雙向數(shù)據(jù)綁定、數(shù)據(jù)代理侃上兩句。但是要是稍微深入一點(diǎn)就有點(diǎn)吃力了。所以這幾天痛下決心研究大量技術(shù)文章(起初嘗試看早期源碼,無(wú)奈vue與jQuery不是一個(gè)層級(jí)的,相比于jQuery,vue是真正意義上的前端框架。只能無(wú)奈棄坑轉(zhuǎn)而看技術(shù)博客),對(duì)vue也算有了一個(gè)管中窺豹的認(rèn)識(shí)。最后嘗試實(shí)踐一下自己學(xué)到的知識(shí),基于數(shù)據(jù)代理、數(shù)據(jù)劫持、模板解析、雙向綁定實(shí)現(xiàn)了一個(gè)小型的vue框架。
溫馨提示:文章是按照每個(gè)模塊的實(shí)現(xiàn)依賴關(guān)系來(lái)進(jìn)行分析的,但是在閱讀的時(shí)候可以按照vue的執(zhí)行順序來(lái)分析,這樣對(duì)初學(xué)者更加的友好。推薦的閱讀順序?yàn)椋簩?shí)現(xiàn)VMVM、數(shù)據(jù)代理、實(shí)現(xiàn)Observe、實(shí)現(xiàn)Complie、實(shí)現(xiàn)Watcher。
源碼:https://github.com/yuliangbin/MVVM
功能演示如下所示:

數(shù)據(jù)代理
以下面這個(gè)模板為例,要替換的根元素“#mvvm-app”內(nèi)只有一個(gè)文本節(jié)點(diǎn)#text,#text的內(nèi)容為{{name}}。我們就以下面這個(gè)模板詳細(xì)了解一下VUE框架的大體實(shí)現(xiàn)流程。
<body>
<div id="mvvm-app">
{{name}}
</div>
<script src="./js/observer.js"></script>
<script src="./js/watcher.js"></script>
<script src="./js/compile.js"></script>
<script src="./js/mvvm.js"></script>
<script>
let vm = new MVVM({
el: "#mvvm-app",
data: {
name: "hello world"
},
})
</script>
</body>
數(shù)據(jù)代理
1、什么是數(shù)據(jù)代理
在vue里面,我們將數(shù)據(jù)寫(xiě)在data對(duì)象中。但是我們?cè)谠L問(wèn)data里的數(shù)據(jù)時(shí),既可以通過(guò)vm.data.name訪問(wèn),也可以通過(guò)vm.name訪問(wèn)。這就是數(shù)據(jù)代理:在一個(gè)對(duì)象中,可以動(dòng)態(tài)的訪問(wèn)和設(shè)置另一個(gè)對(duì)象的屬性。
2、實(shí)現(xiàn)原理
我們知道靜態(tài)綁定(如vm.name = vm.data.name)可以一次性的將結(jié)果賦給變量,而使用Object.defineProperty()方法來(lái)綁定則可以通過(guò)set和get函數(shù)實(shí)現(xiàn)賦值的中間過(guò)程,從而實(shí)現(xiàn)數(shù)據(jù)的動(dòng)態(tài)綁定。具體實(shí)現(xiàn)如下:
let obj = {};
let obj1 = {
name: 'xiaoyu',
age: 18,
}
//實(shí)現(xiàn)origin對(duì)象代理target對(duì)象
function proxyData(origin,target){
Object.keys(target).forEach(function(key){
Object.defineProperty(origin,key,{//定義origin對(duì)象的key屬性
enumerable: false,
configurable: true,
get: function getter(){
return target[key];//origin[key] = target[key];
},
set: function setter(newValue){
target[key] = newValue;
}
})
})
}
vue中的數(shù)據(jù)代理也是通過(guò)這種方式來(lái)實(shí)現(xiàn)的。
function MVVM(options) {
this.$options = options || {};
var data = this._data = this.$options.data;
var _this = this;//當(dāng)前實(shí)例vm
// 數(shù)據(jù)代理
// 實(shí)現(xiàn) vm._data.xxx -> vm.xxx
Object.keys(data).forEach(function(key) {
_this._proxyData(key);
});
observe(data, this);
this.$compile = new Compile(options.el || document.body, this);
}
MVVM.prototype = {
_proxyData: function(key) {
var _this = this;
if (typeof key == 'object' && !(key instanceof Array)){//這里只實(shí)現(xiàn)了對(duì)對(duì)象的監(jiān)聽(tīng),沒(méi)有實(shí)現(xiàn)數(shù)組的
this._proxyData(key);
}
Object.defineProperty(_this, key, {
configurable: false,
enumerable: true,
get: function proxyGetter() {
return _this._data[key];
},
set: function proxySetter(newVal) {
_this._data[key] = newVal;
}
});
},
};
實(shí)現(xiàn)Observe
1、雙向數(shù)據(jù)綁定
數(shù)據(jù)變動(dòng) ---> 視圖更新
視圖更新 ---> 數(shù)據(jù)變動(dòng)
要想實(shí)現(xiàn)當(dāng)數(shù)據(jù)變動(dòng)時(shí)視圖更新,首先要做的就是如何知道數(shù)據(jù)變動(dòng)了,可以通過(guò)Object.defineProperty()函數(shù)監(jiān)聽(tīng)data對(duì)象里的數(shù)據(jù),當(dāng)數(shù)據(jù)變動(dòng)了就會(huì)觸發(fā)set()方法。所以我們需要實(shí)現(xiàn)一個(gè)數(shù)據(jù)監(jiān)聽(tīng)器Observe,來(lái)對(duì)數(shù)據(jù)對(duì)象中的所有屬性進(jìn)行監(jiān)聽(tīng),當(dāng)某一屬性數(shù)據(jù)發(fā)生變化時(shí),拿到最新的數(shù)據(jù)通知綁定了該屬性的訂閱器,訂閱器再執(zhí)行相應(yīng)的數(shù)據(jù)更新回調(diào)函數(shù),從而實(shí)現(xiàn)視圖的刷新。
當(dāng)設(shè)置this.name = 'hello vue'時(shí),就會(huì)執(zhí)行set函數(shù),通知訂閱器里的訂閱者執(zhí)行相應(yīng)的回調(diào)函數(shù),實(shí)現(xiàn)數(shù)據(jù)變動(dòng),對(duì)應(yīng)視圖更新。
function observe(data){
if (typeof data != 'object') {
return ;
}
return new Observe(data);
}
function Observe(data){
this.data = data;
this.walk(data);
}
Observe.prototype = {
walk: function(data){
let _this = this;
for (key in data) {
if (data.hasOwnProperty(key)){
let value = data[key];
if (typeof value == 'object'){
observe(value);
}
_this.defineReactive(data,key,data[key]);
}
}
},
defineReactive: function(data,key,value){
Object.defineProperty(data,key,{
enumerable: true,//可枚舉
configurable: false,//不能再define
get: function(){
console.log('你訪問(wèn)了' + key);return value;
},
set: function(newValue){
console.log('你設(shè)置了' + key);
if (newValue == value) return;
value = newValue;
observe(newValue);//監(jiān)聽(tīng)新設(shè)置的值
}
})
}
}
2、實(shí)現(xiàn)一個(gè)訂閱器
要想通知訂閱者,首先得要有一個(gè)訂閱器(統(tǒng)一管理所有的訂閱者)。為了方便管理,我們會(huì)為每一個(gè)data對(duì)象的屬性都添加一個(gè)訂閱器(new Dep)。
訂閱器里存著的是訂閱者Watcher(后面會(huì)講到),由于訂閱者可能會(huì)有多個(gè),我們需要建立一個(gè)數(shù)組來(lái)維護(hù)。一旦數(shù)據(jù)變化,就會(huì)觸發(fā)訂閱器的notify()方法,訂閱者就會(huì)調(diào)用自身的update方法實(shí)現(xiàn)視圖更新。
function Dep(){
this.subs = [];
}
Dep.prototype = {
addSub: function(sub){this.subs.push(sub);
},
notify: function(){
this.subs.forEach(function(sub) {
sub.update();
})
}
}
每次響應(yīng)屬性的set()函數(shù)調(diào)用的時(shí)候,都會(huì)觸發(fā)訂閱器,所以代碼補(bǔ)充完整。
Observe.prototype = {
//省略的代碼未作更改
defineReactive: function(data,key,value){
let dep = new Dep();//創(chuàng)建一個(gè)訂閱器,會(huì)被閉包在key屬性的get/set函數(shù)內(nèi),因此每個(gè)屬性對(duì)應(yīng)唯一一個(gè)訂閱器dep實(shí)例
Object.defineProperty(data,key,{
enumerable: true,//可枚舉
configurable: false,//不能再define
get: function(){
console.log('你訪問(wèn)了' + key);
return value;
},
set: function(newValue){
console.log('你設(shè)置了' + key);
if (newValue == value) return;
value = newValue;
observe(newValue);//監(jiān)聽(tīng)新設(shè)置的值
dep.notify();//通知所有的訂閱者
}
})
}
}
實(shí)現(xiàn)Complie
compile主要做的事情是解析模板指令,將模板中的data屬性替換成data屬性對(duì)應(yīng)的值(比如將{{name}}替換成data.name值),然后初始化渲染頁(yè)面視圖,并且為每個(gè)data屬性添加一個(gè)監(jiān)聽(tīng)數(shù)據(jù)的訂閱者(new Watcher),一旦數(shù)據(jù)有變動(dòng),收到通知,更新視圖。
遍歷解析需要替換的根元素el下的HTML標(biāo)簽必然會(huì)涉及到多次的DOM節(jié)點(diǎn)操作,因此不可避免的會(huì)引發(fā)頁(yè)面的重排或重繪,為了提高性能和效率,我們把根元素el下的所有節(jié)點(diǎn)轉(zhuǎn)換為文檔碎片fragment進(jìn)行解析編譯操作,解析完成,再將fragment添加回原來(lái)的真實(shí)dom節(jié)點(diǎn)中。
注:文檔碎片本身也是一個(gè)節(jié)點(diǎn),但是當(dāng)將該節(jié)點(diǎn)append進(jìn)頁(yè)面時(shí),該節(jié)點(diǎn)標(biāo)簽作為根節(jié)點(diǎn)不會(huì)顯示html文檔中,其里面的子節(jié)點(diǎn)則可以完全顯示。
Compile解析模板,將模板內(nèi)的子元素#text添加進(jìn)文檔碎片節(jié)點(diǎn)fragment。
function Compile(el,vm){
this.$vm = vm;//vm為當(dāng)前實(shí)例
this.$el = document.querySelector(el);//獲得要解析的根元素
if (this.$el){
this.$fragment = this.nodeToFragment(this.$el);
this.init();
this.$el.appendChild(this.$fragment);
}
}
Compile.prototype = {
nodeToFragment: function(el){
let fragment = document.createDocumentFragment();
let child;
while (child = el.firstChild){
fragment.appendChild(child);//append相當(dāng)于剪切的功能
}
return fragment;
},
};
compileElement方法將遍歷所有節(jié)點(diǎn)及其子節(jié)點(diǎn),進(jìn)行掃描解析編譯,調(diào)用對(duì)應(yīng)的指令渲染函數(shù)進(jìn)行數(shù)據(jù)渲染,并調(diào)用對(duì)應(yīng)的指令更新函數(shù)進(jìn)行綁定,詳看代碼及注釋說(shuō)明:
因?yàn)槲覀兊哪0逯缓幸粋€(gè)文本節(jié)點(diǎn)#text,因此compileElement方法執(zhí)行后會(huì)進(jìn)入_this.compileText(node,reg.exec(node.textContent)[1]);//#text,'name'
Compile.prototype = {
nodeToFragment: function(el){
let fragment = document.createDocumentFragment();
let child;
while (child = el.firstChild){
fragment.appendChild(child);//append相當(dāng)于剪切的功能
}
return fragment;
},
init: function(){
this.compileElement(this.$fragment);
},
compileElement: function(node){
let childNodes = node.childNodes;
const _this = this;
let reg = /\{\{(.*)\}\}/g;
[].slice.call(childNodes).forEach(function(node){
if (_this.isElementNode(node)){//如果為元素節(jié)點(diǎn),則進(jìn)行相應(yīng)操作
_this.compile(node);
} else if (_this.isTextNode(node) && reg.test(node.textContent)){
//如果為文本節(jié)點(diǎn),并且包含data屬性(如{{name}}),則進(jìn)行相應(yīng)操作
_this.compileText(node,reg.exec(node.textContent)[1]);//#text,'name'
}
if (node.childNodes && node.childNodes.length){
//如果節(jié)點(diǎn)內(nèi)還有子節(jié)點(diǎn),則遞歸繼續(xù)解析節(jié)點(diǎn)
_this.compileElement(node);
}
})
},
compileText: function(node,exp){//#text,'name'
compileUtil.text(node,this.$vm,exp);//#text,vm,'name'
},};
CompileText()函數(shù)實(shí)現(xiàn)初始化渲染頁(yè)面視圖(將data.name的值通過(guò)#text.textContent = data.name顯示在頁(yè)面上),并且為每個(gè)DOM節(jié)點(diǎn)添加一個(gè)監(jiān)聽(tīng)數(shù)據(jù)的訂閱者(這里是為#text節(jié)點(diǎn)新增一個(gè)Wather)。
let updater = {
textUpdater: function(node,value){
node.textContent = typeof value == 'undefined' ? '' : value;
},
}
let compileUtil = {
text: function(node,vm,exp){//#text,vm,'name'
this.bind(node,vm,exp,'text');
},
bind: function(node,vm,exp,dir){//#text,vm,'name','text'
let updaterFn = updater[dir + 'Updater'];
updaterFn && updaterFn(node,this._getVMVal(vm,exp));
new Watcher(vm,exp,function(value){
updaterFn && updaterFn(node,value)
});
console.log('加進(jìn)去了');
}
};
現(xiàn)在我們完成了一個(gè)能實(shí)現(xiàn)文本節(jié)點(diǎn)解析的Compile()函數(shù),接下來(lái)我們實(shí)現(xiàn)一個(gè)Watcher()函數(shù)。
實(shí)現(xiàn)Watcher
我們前面講過(guò),Observe()函數(shù)實(shí)現(xiàn)data對(duì)象的屬性劫持,并在屬性值改變時(shí)觸發(fā)訂閱器的notify()通知訂閱者Watcher,訂閱者就會(huì)調(diào)用自身的update方法實(shí)現(xiàn)視圖更新。
Compile()函數(shù)負(fù)責(zé)解析模板,初始化頁(yè)面,并且為每個(gè)data屬性新增一個(gè)監(jiān)聽(tīng)數(shù)據(jù)的訂閱者(new Watcher)。
Watcher訂閱者作為Observer和Compile之間通信的橋梁,所以我們可以大致知道Watcher的作用是什么。
主要做的事情是:
在自身實(shí)例化時(shí)往訂閱器(dep)里面添加自己。
自身必須有一個(gè)update()方法 。
待屬性變動(dòng)dep.notice()通知時(shí),能調(diào)用自身的update()方法,并觸發(fā)Compile中綁定的回調(diào)。
先給出全部代碼,再分析具體的功能。
//Watcher
function Watcher(vm, exp, cb) {
this.vm = vm;
this.cb = cb;
this.exp = exp;
this.value = this.get();//初始化時(shí)將自己添加進(jìn)訂閱器
};
Watcher.prototype = {
update: function(){
this.run();
},
run: function(){
const value = this.vm[this.exp];
//console.log('me:'+value);
if (value != this.value){
this.value = value;
this.cb.call(this.vm,value);
}
},
get: function() {
Dep.target = this; // 緩存自己
var value = this.vm[this.exp] // 訪問(wèn)自己,執(zhí)行defineProperty里的get函數(shù)
Dep.target = null; // 釋放自己
return value;
}
}
//這里列出Observe和Dep,方便理解
Observe.prototype = {
defineReactive: function(data,key,value){
let dep = new Dep();
Object.defineProperty(data,key,{
enumerable: true,//可枚舉
configurable: false,//不能再define
get: function(){
console.log('你訪問(wèn)了' + key);
//說(shuō)明這是實(shí)例化Watcher時(shí)引起的,則添加進(jìn)訂閱器
if (Dep.target){
//console.log('訪問(wèn)了Dep.target');
dep.addSub(Dep.target);
}
return value;
},
})
}
}
Dep.prototype = {
addSub: function(sub){this.subs.push(sub);
},
}
我們知道在Observe()函數(shù)執(zhí)行時(shí),我們?yōu)槊總€(gè)屬性都添加了一個(gè)訂閱器dep,而這個(gè)dep被閉包在屬性的get/set函數(shù)內(nèi)。所以,我們可以在實(shí)例化Watcher時(shí)調(diào)用this.get()函數(shù)訪問(wèn)data.name屬性,這會(huì)觸發(fā)defineProperty()函數(shù)內(nèi)的get函數(shù),get方法執(zhí)行的時(shí)候,就會(huì)在屬性的訂閱器dep添加當(dāng)前watcher實(shí)例,從而在屬性值有變化的時(shí)候,watcher實(shí)例就能收到更新通知。
那么Watcher()函數(shù)中的get()函數(shù)內(nèi)Dep.taeger = this又有什么特殊的含義呢?我們希望的是在實(shí)例化Watcher時(shí)將相應(yīng)的Watcher實(shí)例添加一次進(jìn)dep訂閱器即可,而不希望在以后每次訪問(wèn)data.name屬性時(shí)都加入一次dep訂閱器。所以我們?cè)趯?shí)例化執(zhí)行this.get()函數(shù)時(shí)用Dep.target = this來(lái)標(biāo)識(shí)當(dāng)前Watcher實(shí)例,當(dāng)添加進(jìn)dep訂閱器后設(shè)置Dep.target=null。
實(shí)現(xiàn)VMVM
MVVM作為數(shù)據(jù)綁定的入口,整合Observer、Compile和Watcher三者,通過(guò)Observer來(lái)監(jiān)聽(tīng)自己的model數(shù)據(jù)變化,通過(guò)Compile來(lái)解析編譯模板指令,最終利用Watcher搭起Observer和Compile之間的通信橋梁,達(dá)到數(shù)據(jù)變化 -> 視圖更新;視圖交互變化(input) -> 數(shù)據(jù)model變更的雙向綁定效果。
function MVVM(options) {
this.$options = options || {};
var data = this._data = this.$options.data;
var _this = this;
// 數(shù)據(jù)代理
// 實(shí)現(xiàn) vm._data.xxx -> vm.xxx
Object.keys(data).forEach(function(key) {
_this._proxyData(key);
});
observe(data, this);
this.$compile = new Compile(options.el || document.body, this);
}
- Android單項(xiàng)綁定MVVM項(xiàng)目模板的方法
- 淺析vue中的MVVM實(shí)現(xiàn)原理
- vue中的mvvm模式講解
- 如何在iOS上使用MVVM進(jìn)行路由詳解
- MVVM 雙向綁定的實(shí)現(xiàn)代碼
- MVC、MVP和MVVM分別是什么_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
- MVVM模式下WPF動(dòng)態(tài)綁定展示圖片
- 詳解Android的MVVM框架 - 數(shù)據(jù)綁定
- JavaScript的MVVM庫(kù)Vue.js入門(mén)學(xué)習(xí)筆記
- vue,angular,avalon這三種MVVM框架優(yōu)缺點(diǎn)
- 詳解Android框架MVVM分析以及使用
相關(guān)文章
Vue導(dǎo)入excel文件的兩種方式(form表單和el-upload)
最近開(kāi)發(fā)遇到一個(gè)點(diǎn)擊導(dǎo)入按鈕讓excel文件數(shù)據(jù)導(dǎo)入的需求,下面這篇文章主要給大家介紹了關(guān)于Vue導(dǎo)入excel文件的兩種方式,分別是form表單和el-upload兩種方法,需要的朋友可以參考下2022-11-11
Vue入門(mén)實(shí)戰(zhàn)之天氣預(yù)報(bào)
這篇文章主要為大家詳細(xì)介紹了Vue入門(mén)實(shí)戰(zhàn)之天氣預(yù)報(bào),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
lottie實(shí)現(xiàn)vue自定義loading指令及常用指令封裝詳解
這篇文章主要為大家介紹了lottie實(shí)現(xiàn)vue自定義loading指令及常用指令封裝,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Vue 通過(guò)公共字段,拼接兩個(gè)對(duì)象數(shù)組的實(shí)例
今天小編就為大家分享一篇Vue 通過(guò)公共字段,拼接兩個(gè)對(duì)象數(shù)組的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
vue組件 keep-alive 和 transition 使用詳解
這篇文章主要介紹了vue組件 keep-alive 和 transition 使用詳解,需要的朋友可以參考下2019-10-10
結(jié)合mint-ui移動(dòng)端下拉加載實(shí)踐方法總結(jié)
下面小編就為大家?guī)?lái)一篇結(jié)合mint-ui移動(dòng)端下拉加載實(shí)踐方法總結(jié)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
vue el-tree 默認(rèn)展開(kāi)第一個(gè)節(jié)點(diǎn)的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue el-tree 默認(rèn)展開(kāi)第一個(gè)節(jié)點(diǎn)的實(shí)現(xiàn)代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
基于Vue3+Element Plus 實(shí)現(xiàn)多表單校驗(yàn)demo
表單校驗(yàn)在日常的開(kāi)發(fā)需求中是一種很常見(jiàn)的需求,通常在提交表單發(fā)起請(qǐng)求前校驗(yàn)用戶輸入是否符合規(guī)則,通常只需formRef.value.validate()即可校驗(yàn),本文給大家介紹基于Vue3+Element Plus 實(shí)現(xiàn)多表單校驗(yàn)demo,感興趣的朋友一起看看吧2024-06-06

