一、程序运行过程
1、runtime-compiler中vue的运行过程
![](https://img.haomeiwen.com/i5965079/aa0ccc99c63a362e.jpg)
如上图所示:
- 1、将
vue
中的模板解析成abstract syntax tree(art)
抽象语法树。- 2、将语法树编译成
render
函数。- 3、将
render
函数翻译成virtual dom
虚拟DOM
。- 4、将虚拟
DOM
渲染到浏览器上。
2、runtime-only
- 基于
vue-template-compiler
开发依赖,可以将vue
文件中的template
解析成render
函数;所以它只有上面的3、4步骤。
二、区别
- 1、
runtime-only
比runtime-compiler
轻6kb(创建时,官方文档说明有)。 - 2、
runtime-only
的运行速度相较于runtime-compiler
更快。 -
runtime-compiler
可以识别.html
文件中的template
,而runtime-only
不能识别template
,只能识别render
函数,.vue
文件中的template
也是被开发依赖vue-template-compiler
翻译成render
函数,所以只能在.vue中
写template
。而该依赖为开发依赖,并不在最后生产中,所以最后生产出来的代码是没有template
。
原因:如上图中的程序运行过程,
runtime-only
比runtime-compiler
少了1、2两个步骤,解析和编译需要时间,且需要注入相应的方法以便于解析和编译;runtime-only
轻、快的原因也就在于此。
3、两者之间主要的区别体现main.js
中,如以下两张图片:
-
8C32205EFF901A18E134992E1A042771.jpg
F8C059DE68ED0F26B696726BFC97AD6F.jpg
在runtime-only
模板中的的函数
render: h => h(App)
其实质为:
render:function(createElement){
return createElement(App)
}
ES6简写:
render (createElement) {
return createElement(App);
}
再进一步缩写为:
render (h){
return h(App);
}
通过箭头函数就有了:
render: h => h(App);
网友评论