Vue之Watcher源碼解析(1)
上一節(jié)最后再次調(diào)用了mount函數(shù),我發(fā)現(xiàn)竟然跳到了7000多行的那個(gè)函數(shù),之前我還說(shuō)因?yàn)槁暶髟缌吮桓采w,看來(lái)我錯(cuò)了!
就是這個(gè)函數(shù):
// Line-7531 Vue$3.prototype.$mount = function(el, hydrating) { el = el && inBrowser ? query(el) : undefined; return mountComponent(this, el, hydrating) };
第一步query就不用看了,el此時(shí)是一個(gè)DOM節(jié)點(diǎn),所以直接返回,然后調(diào)用了mountComponent函數(shù)。
// Line-2375 function mountComponent(vm, el, hydrating) { vm.$el = el; /* 檢測(cè)vm.$options.render */ // 調(diào)用鉤子函數(shù) callHook(vm, 'beforeMount'); var updateComponent; /* istanbul ignore if */ if ("development" !== 'production' && config.performance && mark) { /* 標(biāo)記vue-perf */ } else { updateComponent = function() { vm._update(vm._render(), hydrating); }; } // 生成中間件watcher vm._watcher = new Watcher(vm, updateComponent, noop); hydrating = false; // 調(diào)用最后一個(gè)鉤子函數(shù) if (vm.$vnode == null) { vm._isMounted = true; callHook(vm, 'mounted'); } return vm }
這個(gè)函數(shù)做了三件事,調(diào)用beforeMount鉤子函數(shù),生成Watcher對(duì)象,接著調(diào)用mounted鉤子函數(shù)。
數(shù)據(jù)雙綁、AST對(duì)象處理完后,這里的Watcher對(duì)象負(fù)責(zé)將兩者聯(lián)系到一起,上一張網(wǎng)上的圖片:
可以看到,之前以前把所有的組件都過(guò)了一遍,目前就剩一個(gè)Watcher了。
構(gòu)造新的Watcher對(duì)象傳了3個(gè)參數(shù),當(dāng)前vue實(shí)例、updateComponent函數(shù)、空函數(shù)。
// Line-2697 var Watcher = function Watcher(vm, expOrFn, cb, options) { this.vm = vm; // 當(dāng)前Watcher添加到vue實(shí)例上 vm._watchers.push(this); // 參數(shù)配置 默認(rèn)為false if (options) { this.deep = !!options.deep; this.user = !!options.user; this.lazy = !!options.lazy; this.sync = !!options.sync; } else { this.deep = this.user = this.lazy = this.sync = false; } this.cb = cb; this.id = ++uid$2; this.active = true; this.dirty = this.lazy; // for lazy watchers this.deps = []; this.newDeps = []; // 內(nèi)容不可重復(fù)的數(shù)組對(duì)象 this.depIds = new _Set(); this.newDepIds = new _Set(); // 把函數(shù)變成字符串形式` this.expression = expOrFn.toString(); // parse expression for getter if (typeof expOrFn === 'function') { this.getter = expOrFn; } else { this.getter = parsePath(expOrFn); if (!this.getter) { this.getter = function() {}; "development" !== 'production' && warn( "Failed watching path: \"" + expOrFn + "\" " + 'Watcher only accepts simple dot-delimited paths. ' + 'For full control, use a function instead.', vm ); } } // 不是懶加載類型調(diào)用get this.value = this.lazy ? undefined : this.get(); };
該構(gòu)造函數(shù)添加了一堆屬性,第二個(gè)參數(shù)由于是函數(shù),直接作為getter屬性加到watcher上,將字符串后則作為expression屬性。
最后有一個(gè)value屬性,由于lazy為false,調(diào)用原型函數(shù)gei進(jìn)行賦值:
// Line-2746 Watcher.prototype.get = function get() { pushTarget(this); var value; var vm = this.vm; if (this.user) { try { value = this.getter.call(vm, vm); } catch (e) { handleError(e, vm, ("getter for watcher \"" + (this.expression) + "\"")); } } else { // 調(diào)用之前的updateComponent value = this.getter.call(vm, vm); } // "touch" every property so they are all tracked as // dependencies for deep watching if (this.deep) { traverse(value); } popTarget(); this.cleanupDeps(); return value }; // Line-750 Dep.target = null; var targetStack = []; function pushTarget(_target) { // 默認(rèn)為null if (Dep.target) { targetStack.push(Dep.target); } // 依賴目前標(biāo)記為當(dāng)前watcher Dep.target = _target; } function popTarget() { Dep.target = targetStack.pop(); }
原型方法get中,先設(shè)置了依賴收集數(shù)組Dep的target值,user屬性暫時(shí)不清楚意思,跳到了else分支,調(diào)用了getter函數(shù)。而getter就是之前的updateComponent函數(shù):
// Line-2422 updateComponent = function() { vm._update(vm._render(), hydrating); };
這個(gè)函數(shù)不接受參數(shù),所以說(shuō)傳進(jìn)來(lái)的兩個(gè)vm并沒(méi)有什么卵用,調(diào)用這個(gè)函數(shù)會(huì)接著調(diào)用_update函數(shù),這個(gè)是掛載到vue原型的方法:
// Line-2422 Vue.prototype._render = function() { var vm = this; var ref = vm.$options; var render = ref.render; var staticRenderFns = ref.staticRenderFns; var _parentVnode = ref._parentVnode; // 檢測(cè)是否已掛載 if (vm._isMounted) { // clone slot nodes on re-renders for (var key in vm.$slots) { vm.$slots[key] = cloneVNodes(vm.$slots[key]); } } // 都沒(méi)有 vm.$scopedSlots = (_parentVnode && _parentVnode.data.scopedSlots) || emptyObject; if (staticRenderFns && !vm._staticTrees) { vm._staticTrees = []; } vm.$vnode = _parentVnode; // render self var vnode; try { // 調(diào)用之前的render字符串函數(shù) vnode = render.call(vm._renderProxy, vm.$createElement); } catch (e) { /* handler error */ } // return empty vnode in case the render function errored out if (!(vnode instanceof VNode)) { /* 報(bào)錯(cuò) */ vnode = createEmptyVNode(); } // set parent vnode.parent = _parentVnode; return vnode };
方法獲取了一些vue實(shí)例的參數(shù),比較重點(diǎn)的是render函數(shù),調(diào)用了之前字符串后的ast對(duì)象:
在這里有點(diǎn)不一樣的地方,接下來(lái)的跳轉(zhuǎn)有點(diǎn)蒙,下節(jié)再說(shuō)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于vue-cli-service:command?not?found報(bào)錯(cuò)引發(fā)的實(shí)戰(zhàn)案例
這篇文章主要給大家介紹了關(guān)于vue-cli-service:command?not?found報(bào)錯(cuò)引發(fā)的相關(guān)資料,文中通過(guò)實(shí)例代碼將解決的過(guò)程介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-02-02Vue之Element級(jí)聯(lián)選擇器多選傳值以及回顯方式(樹(shù)形結(jié)構(gòu))
這篇文章主要介紹了Vue之Element級(jí)聯(lián)選擇器多選傳值以及回顯方式(樹(shù)形結(jié)構(gòu)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07Vue結(jié)合Video.js播放m3u8視頻流的方法示例
本篇文章主要介紹了Vue+Video.js播放m3u8視頻流的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05Vue中引入echarts的步驟及折線圖、柱狀圖常見(jiàn)配置項(xiàng)
這篇文章主要介紹了Vue中引入echarts的步驟及折線圖、柱狀圖常見(jiàn)配置項(xiàng),需要的朋友可以參考下2023-11-11vue el-select綁定對(duì)象時(shí),回顯內(nèi)容不正確,始終是最后一項(xiàng)的解決
這篇文章主要介紹了vue el-select綁定對(duì)象時(shí),回顯內(nèi)容不正確,始終是最后一項(xiàng)的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07vue-cli 2.*中導(dǎo)入公共less文件的方法步驟
這篇文章主要介紹了vue-cli 2.*中導(dǎo)入公共less文件的方法步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11去掉vue 中的代碼規(guī)范檢測(cè)兩種方法(Eslint驗(yàn)證)
我們?cè)谑褂胿ue 腳手架時(shí),為了規(guī)范團(tuán)隊(duì)的代碼格式,會(huì)有一個(gè)代碼規(guī)范檢測(cè),如果不符合規(guī)范就會(huì)報(bào)錯(cuò),有時(shí)候我們不想按照他的規(guī)范去寫(xiě)。這時(shí)我們需要關(guān)閉,這里腳本之家小編給大家?guī)?lái)了去掉vue 中的代碼規(guī)范檢測(cè)兩種方法(Eslint驗(yàn)證),一起看看吧2018-03-03