vue38 —— runtime+compiler & runtime-only

本文探讨了Vue程序的运行过程,重点比较了使用runtime compiler和runtime-only模式的区别。在runtime compiler中,template经过编译生成render函数,然后转化为虚拟DOM并更新UI。而runtime-only模式下,.vue文件的template预先由vue-template-compiler处理,直接生成render函数,具有更高的性能和更小的代码体积。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vue程序运行过程:


template —解析–> ast –编译–>render函数 --^virtual tree^–> UI

vue init webpack myproject 之后,有Vue build 选项: runtime compiler 和 runtime-only

两种选择生成的项目区别,只在main.js中

runtime compiler - main.js:

import Vue from 'vue'
import App from './App'

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  components: { App },
  template: '<App/>'
})

template > ast > render > vdom > ui


runtime-only - main.js:

import Vue from 'vue'
import App from './App'

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  render: h => h(App)
})

.vue文件中的template是由 vue-template-compiler 处理
render > vdom > ui

runtime-only 的性能更高,代码量更少

render函数

...
render: h => h(App)
// 实际上
render: function(createElement){
// 1. createElement('标签',{标签的属性},[])  
// 是一个函数,接受三个参数:
// 第一个参数,是标签名,会生成对应标签,替代vue实例挂载的dom
// 第二个参数,对象,存放标签的属性 eg. {class: box},可不写
// 第三个参数,数组,存放内容 ['hello world']
// 1. 普通用法
	return createElement('h2',{id: box},['hello, render']) --常规使用
	return createElement('h2',
		{id: box},
		['hello, render',createElement('button',['按钮']])
// 2. 传入一个组件
	return createElement(cpn)
}

### 关于 `vue-template-compiler@2.6.10` 和 Babel Loader 的兼容性 为了确保项目中的工具链能够正常工作,了解不同依赖之间的版本兼容性至关重要。以下是关于 `vue-template-compiler@2.6.10` 和 Babel Loader 版本兼容性的分析。 #### Vue Template CompilerVue 运行时的关系 `vue-template-compiler` 是 Vue 编译器的一部分,负责将 `.vue` 文件中的模板部分转换为渲染函数。它通常需要与特定版本的 Vue Runtime (`vue`) 配合使用,二者应保持相同的主版本号和次版本号以避免不兼容问题[^1]。因此,在使用 `vue-template-compiler@2.6.10` 时,建议同时安装 `vue@2.6.x` 系列版本作为运行时库。 #### Webpack 中的 Vue 加载流程 在基于 Webpack 的构建环境中,`.vue` 文件通过 `vue-loader` 被解析并处理。而 `vue-loader` 可能会调用到 `babel-loader` 来进一步编译 JavaScript 或 TypeScript 代码片段。这意味着 `babel-loader` 的版本选择会影响整个项目的构建过程。 对于 `vue-template-compiler@2.6.10` 所支持的环境而言,推荐使用的 `babel-loader` 版本范围如下: - **Babel Loader v7**: 如果您正在使用 Babel 7,则应该选用 `babel-loader@7.x` 或更高版本(如 `babel-loader@8.x`)。这是因为 Babel 7 提供了许多新特性和改进,适合现代前端开发需求。 具体来说,当您的项目已经锁定至 Vue 2.6 并希望维持稳定性时,可以选择以下组合之一: - `babel-loader@7.1.5`: 已被广泛验证适用于大多数场景,并且性能表现良好[^4]。 - `babel-loader@8.x`: 更新的选项,默认情况下也完全适配 Vue 2.x 生态系统[^4]。 需要注意的是,尽管理论上较新版的 `babel-loader` 应该向后兼容旧版功能,但在实际应用过程中仍需测试是否存在潜在冲突。 #### 安装命令示例 假设我们决定采用最新稳定版的 `babel-loader`, 下面是如何执行安装操作的具体方法: ```bash npm install vue@2.6.10 vue-template-compiler@2.6.10 babel-loader@latest --save-dev ``` 或者如果偏好指定确切的小版本号来减少不确定性风险的话, ```bash npm install vue@2.6.10 vue-template-compiler@2.6.10 babel-loader@7.1.5 --save-dev ``` 以上两条指令均会在当前目录下的 package.json 文件里记录下所选软件包及其对应版本信息. #### 总结 综上所述, 推荐搭配 `vue-template-compiler@2.6.10` 使用的 `babel-loader` 最佳实践方案包括但不限于: - 若追求长期维护和支持度较高的解决方案, 则优先考虑 `babel-loader@7.1.5`. - 如倾向于利用更新特性以及享受更频繁的安全补丁发布周期带来的好处, 则可以直接升级至最新的 `babel-loader@8.x`. 最终的选择取决于团队的技术栈现状和个人喜好等因素综合考量后的结果。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值