runtime-only和runtime-compiler

runtime-only和runtime-compiler


博客内容来自coderwhy老师的vue.js视频,进行学习总结。

一、解析过程的差异

// Runtime-Complier 解析过程:
1、将template模板解析成抽象语法树(ast)抽象语法树;
2、将抽象语法树编译成render函数;
3、将render函数再翻译成virtual dom (虚拟dom);
4、将虚拟dom转换成真正的DOM并挂载到浏览器上;

// Runtime-only 解析过程:
1、通过loader:vue-template-compiler 直接将组件编译成 render函数;
2、将render函数再翻译成virtual dom (虚拟dom);
3、将虚拟dom转换成真正的DOM并挂载到浏览器上;

二、性能差异

1、runtime-only比起runtime-compiler由于在解析过程中由所省略,所以性能更高。
2、runtime-only比runtime-compiler代码量更少,约6kb左右。

//runtime compiler
import Vue from 'vue'
import App from './App'
new Vue({
	el:'#app',
	template:'<App/>',
	components: {App}
})


//runtime only
import Vue from 'vue'
import App from './App'
new Vue({
	el:'#app',
	render:h=>h(App)
})
/*或:
new Vue({
	render:h=>h(App),
}).$mount('#app')
*/

runtime only中:
①:
render:h=>h(App) 是箭头函数,还原到es5是:
render: function(h){ return h(App) }
②:
其中的 h 是一个函数:createElement()
所以:render: function(createElement){ return createElement(App) }
③:关于 createElement()
1、普通用法 return createElement(‘标签名’,{标签属性},[‘标签内容’])
render: function(createElement){
    return createElement(‘p’,
    {class:‘box’},
    [‘hello world!’,createElement(‘button’,[‘按钮’])])

2、传入组件对象用法
return createElement(App)

new Vue({
  router,
  render: h => h(App)
  /* render: function(h){
	  return h(App)
  } */
  /* render: function(createElement){
	  //普通用法 return createElement('标签名',{标签属性},['标签内容'])
	  return createElement('p',
		{class:'box'},
		['hello world!',createElement('button',['按钮'])])
		
	  //传入组件对象用法
	  return createElement(App)
  } */
}).$mount('#app')

最后贴上coderwhy老师的图:解析过程图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值