摘要:模板轉(zhuǎn)換成瀏覽器認識的過程如下解析方法運行這里總結(jié)下第一步模板編譯成函數(shù)的方式,生成的函數(shù)都會加在實例的上或者原型上,調(diào)用實例的方法時被調(diào)用。
模板轉(zhuǎn)換成瀏覽器認識的HTML過程如下:
template -> AST render (compiler解析template)
AST render -> vNode (render方法運行)
vNode -> DOM (vdom.patch)
這里總結(jié)下第一步模板編譯成AST render函數(shù)的方式,生成的render函數(shù)都會加在vue實例的$options上或者$options原型上,調(diào)用實例的_render方法時被調(diào)用。可以看vue的源碼:
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ù)
這也是在使用打包工具時,只需引入運行時的vue, 如vue/dist/vue.runtime.esm.js
單文件 HelloWorld.vue
{{ msg }}
測試文件main.js
import HelloWorld from "./components/HelloWorld" console.log("===========vue-template-compiler===========") console.log(HelloWorld);
單文件HelloWorld.vue經(jīng)過vue-loader處理,由vue-template-compiler編譯生成的render函數(shù):
詳細內(nèi)容如下圖,里面的_h, _c, _v, _s都是vue實例函數(shù)的簡稱
import Vue from "vue" let templateHelloworld = `{{ msg }}` // Vue完整版里的compiler let vueCompilerResult = Vue.compile(templateHelloworld); console.log("===========vue compiler===========") console.log(vueCompilerResult);
這是一個匿名函數(shù),with包裹作用域,_c同上,是vue實例的方法簡寫
3 用jsx寫render函數(shù),經(jīng)過編譯后的結(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)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/106556.html
摘要:原文鏈接我的,歡迎。這次想要分享的一篇文章是從一個奇怪的錯誤出發(fā)理解的基本概念。瞬間明白了,原來是函數(shù),一個考驗編程能力的函數(shù),比更接近編譯器。來看這里有一個小知識點被忽略在實例掛載之后,元素可以用訪問腦補會用到的場景中。。。 原文鏈接我的blog,歡迎STAR。 這次想要分享的一篇文章是:從一個奇怪的錯誤出發(fā)理解Vue的基本概念。 這篇文章以Vue的兩種構(gòu)建方式做為切入點,深入探討...
摘要:源碼是選用了作為,看的源碼時發(fā)現(xiàn)對應了不同的構(gòu)建選項。這也對應了最后打包構(gòu)建后產(chǎn)出的不同的包。第四種構(gòu)建方式對應的構(gòu)建腳本為不同于前面種構(gòu)建方式這一構(gòu)建對應于將關(guān)于模板編譯的成函數(shù)的單獨進行打包輸出。 Vue源碼是選用了rollup作為bundler,看Vue的源碼時發(fā)現(xiàn):npm script對應了不同的構(gòu)建選項。這也對應了最后打包構(gòu)建后產(chǎn)出的不同的包。 不同于其他的library,V...
摘要:圖在中應用三數(shù)據(jù)渲染過程數(shù)據(jù)綁定實現(xiàn)邏輯本節(jié)正式分析從到數(shù)據(jù)渲染到頁面的過程,在中定義了一個的構(gòu)造函數(shù)。一、概述 vue已是目前國內(nèi)前端web端三分天下之一,也是工作中主要技術(shù)棧之一。在日常使用中知其然也好奇著所以然,因此嘗試閱讀vue源碼并進行總結(jié)。本文旨在梳理初始化頁面時data中的數(shù)據(jù)是如何渲染到頁面上的。本文將帶著這個疑問一點點追究vue的思路??傮w來說vue模版渲染大致流程如圖1所...
摘要:頁面這個實例,按理就需要解析兩次,但是有緩存之后就不會理清思路也就是說,其實內(nèi)核就是不過是經(jīng)過了兩波包裝的第一波包裝在中的內(nèi)部函數(shù)中內(nèi)部函數(shù)的作用是合并公共和自定義,但是相關(guān)代碼已經(jīng)省略,另一個就是執(zhí)行第二波包裝在中,目的是進行緩存 寫文章不容易,點個贊唄兄弟 專注 Vue 源碼分享,文章分為白話版和 源碼版,白話版助于理解工作原理,源碼版助于了解內(nèi)部詳情,讓我們一起學習吧研究基于 ...
摘要:的構(gòu)造函數(shù)將自動運行啟動函數(shù)。我在閱讀源碼的過程中,發(fā)現(xiàn)源碼余行,而和模板編譯相關(guān)的代碼,則約有行左右。這個是創(chuàng)建的方法,作為第一個參數(shù)傳入。最后會返回一個節(jié)點。這個時候?qū)①x值為這個節(jié)點,掛載完成 mount, 意思為掛載。可以理解為將vue實例(邏輯應用),掛靠在某個dom元素(載體)上的一個過程。 一、創(chuàng)建Vue實例時的渲染過程 上一篇文章我們講到, 在創(chuàng)建一個vue實例的時候(v...
閱讀 1702·2021-09-26 09:55
閱讀 1445·2021-09-23 11:22
閱讀 2812·2021-09-06 15:02
閱讀 2712·2021-09-01 11:43
閱讀 4083·2021-08-27 13:10
閱讀 3748·2021-08-12 13:24
閱讀 2125·2019-08-30 12:56
閱讀 3058·2019-08-30 11:22