摘要:模板轉(zhuǎn)換成瀏覽器認(rèn)識(shí)的過(guò)程如下解析方法運(yùn)行這里總結(jié)下第一步模板編譯成函數(shù)的方式,生成的函數(shù)都會(huì)加在實(shí)例的上或者原型上,調(diào)用實(shí)例的方法時(shí)被調(diào)用。
模板轉(zhuǎn)換成瀏覽器認(rèn)識(shí)的HTML過(guò)程如下:
template -> AST render (compiler解析template)
AST render -> vNode (render方法運(yùn)行)
vNode -> DOM (vdom.patch)
這里總結(jié)下第一步模板編譯成AST render函數(shù)的方式,生成的render函數(shù)都會(huì)加在vue實(shí)例的$options上或者$options原型上,調(diào)用實(shí)例的_render方法時(shí)被調(diào)用??梢钥磛ue的源碼:
Vue.prototype._render = function () { var vm = this; var ref = vm.$options; var render = ref.render; var _parentVnode = ref._parentVnode; if (_parentVnode) { vm.$scopedSlots = normalizeScopedSlots( _parentVnode.data.scopedSlots, vm.$slots, vm.$scopedSlots ); } // set parent vnode. this allows render functions to have access // to the data on the placeholder node. vm.$vnode = _parentVnode; // render self var vnode; try { // There"s no need to maintain a stack becaues all render fns are called // separately from one another. Nested component"s render fns are called // when parent component is patched. currentRenderingInstance = vm; vnode = render.call(vm._renderProxy, vm.$createElement); } catch (e) { handleError(e, vm, "render"); // return error render result, // or previous vnode to prevent render error causing blank component /* istanbul ignore else */ if (process.env.NODE_ENV !== "production" && vm.$options.renderError) { try { vnode = vm.$options.renderError.call(vm._renderProxy, vm.$createElement, e); } catch (e) { handleError(e, vm, "renderError"); vnode = vm._vnode; } } else { vnode = vm._vnode; } } finally { currentRenderingInstance = null; } // if the returned array contains only a single node, allow it if (Array.isArray(vnode) && vnode.length === 1) { vnode = vnode[0]; } // return empty vnode in case the render function errored out if (!(vnode instanceof VNode)) { if (process.env.NODE_ENV !== "production" && Array.isArray(vnode)) { warn( "Multiple root nodes returned from render function. Render function " + "should return a single root node.", vm ); } vnode = createEmptyVNode(); } // set parent vnode.parent = _parentVnode; return vnode }; }1. webpack打包工具,引入的"vue-template-compiler"編譯生成render函數(shù)
這也是在使用打包工具時(shí),只需引入運(yùn)行時(shí)的vue, 如vue/dist/vue.runtime.esm.js
單文件 HelloWorld.vue
{{ msg }}
測(cè)試文件main.js
import HelloWorld from "./components/HelloWorld" console.log("===========vue-template-compiler===========") console.log(HelloWorld);
單文件HelloWorld.vue經(jīng)過(guò)vue-loader處理,由vue-template-compiler編譯生成的render函數(shù):
詳細(xì)內(nèi)容如下圖,里面的_h, _c, _v, _s都是vue實(shí)例函數(shù)的簡(jiǎn)稱(chēng)
import Vue from "vue" let templateHelloworld = `{{ msg }}` // Vue完整版里的compiler let vueCompilerResult = Vue.compile(templateHelloworld); console.log("===========vue compiler===========") console.log(vueCompilerResult);
這是一個(gè)匿名函數(shù),with包裹作用域,_c同上,是vue實(shí)例的方法簡(jiǎn)寫(xiě)
3 用jsx寫(xiě)render函數(shù),經(jīng)過(guò)編譯后的結(jié)果:import Vue from "vue" let jsxHellowWorld = new Vue({ render: function(h){ return({msg}) } }) console.log("===========jsx===========") console.log(jsxHellowWorld.$options.render);
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/106556.html
摘要:原文鏈接我的,歡迎。這次想要分享的一篇文章是從一個(gè)奇怪的錯(cuò)誤出發(fā)理解的基本概念。瞬間明白了,原來(lái)是函數(shù),一個(gè)考驗(yàn)編程能力的函數(shù),比更接近編譯器。來(lái)看這里有一個(gè)小知識(shí)點(diǎn)被忽略在實(shí)例掛載之后,元素可以用訪問(wèn)腦補(bǔ)會(huì)用到的場(chǎng)景中。。。 原文鏈接我的blog,歡迎STAR。 這次想要分享的一篇文章是:從一個(gè)奇怪的錯(cuò)誤出發(fā)理解Vue的基本概念。 這篇文章以Vue的兩種構(gòu)建方式做為切入點(diǎn),深入探討...
摘要:源碼是選用了作為,看的源碼時(shí)發(fā)現(xiàn)對(duì)應(yīng)了不同的構(gòu)建選項(xiàng)。這也對(duì)應(yīng)了最后打包構(gòu)建后產(chǎn)出的不同的包。第四種構(gòu)建方式對(duì)應(yīng)的構(gòu)建腳本為不同于前面種構(gòu)建方式這一構(gòu)建對(duì)應(yīng)于將關(guān)于模板編譯的成函數(shù)的單獨(dú)進(jìn)行打包輸出。 Vue源碼是選用了rollup作為bundler,看Vue的源碼時(shí)發(fā)現(xiàn):npm script對(duì)應(yīng)了不同的構(gòu)建選項(xiàng)。這也對(duì)應(yīng)了最后打包構(gòu)建后產(chǎn)出的不同的包。 不同于其他的library,V...
摘要:圖在中應(yīng)用三數(shù)據(jù)渲染過(guò)程數(shù)據(jù)綁定實(shí)現(xiàn)邏輯本節(jié)正式分析從到數(shù)據(jù)渲染到頁(yè)面的過(guò)程,在中定義了一個(gè)的構(gòu)造函數(shù)。一、概述 vue已是目前國(guó)內(nèi)前端web端三分天下之一,也是工作中主要技術(shù)棧之一。在日常使用中知其然也好奇著所以然,因此嘗試閱讀vue源碼并進(jìn)行總結(jié)。本文旨在梳理初始化頁(yè)面時(shí)data中的數(shù)據(jù)是如何渲染到頁(yè)面上的。本文將帶著這個(gè)疑問(wèn)一點(diǎn)點(diǎn)追究vue的思路??傮w來(lái)說(shuō)vue模版渲染大致流程如圖1所...
摘要:頁(yè)面這個(gè)實(shí)例,按理就需要解析兩次,但是有緩存之后就不會(huì)理清思路也就是說(shuō),其實(shí)內(nèi)核就是不過(guò)是經(jīng)過(guò)了兩波包裝的第一波包裝在中的內(nèi)部函數(shù)中內(nèi)部函數(shù)的作用是合并公共和自定義,但是相關(guān)代碼已經(jīng)省略,另一個(gè)就是執(zhí)行第二波包裝在中,目的是進(jìn)行緩存 寫(xiě)文章不容易,點(diǎn)個(gè)贊唄兄弟 專(zhuān)注 Vue 源碼分享,文章分為白話版和 源碼版,白話版助于理解工作原理,源碼版助于了解內(nèi)部詳情,讓我們一起學(xué)習(xí)吧研究基于 ...
摘要:的構(gòu)造函數(shù)將自動(dòng)運(yùn)行啟動(dòng)函數(shù)。我在閱讀源碼的過(guò)程中,發(fā)現(xiàn)源碼余行,而和模板編譯相關(guān)的代碼,則約有行左右。這個(gè)是創(chuàng)建的方法,作為第一個(gè)參數(shù)傳入。最后會(huì)返回一個(gè)節(jié)點(diǎn)。這個(gè)時(shí)候?qū)①x值為這個(gè)節(jié)點(diǎn),掛載完成 mount, 意思為掛載??梢岳斫鉃閷ue實(shí)例(邏輯應(yīng)用),掛靠在某個(gè)dom元素(載體)上的一個(gè)過(guò)程。 一、創(chuàng)建Vue實(shí)例時(shí)的渲染過(guò)程 上一篇文章我們講到, 在創(chuàng)建一個(gè)vue實(shí)例的時(shí)候(v...
閱讀 1653·2021-09-26 09:55
閱讀 1386·2021-09-23 11:22
閱讀 2749·2021-09-06 15:02
閱讀 2658·2021-09-01 11:43
閱讀 3977·2021-08-27 13:10
閱讀 3693·2021-08-12 13:24
閱讀 2080·2019-08-30 12:56
閱讀 3009·2019-08-30 11:22