据了解,此版本包括许多重要的新功能和性能改进,但是不包含较重大的更改。
SFC 的新功能
Single File Components(SFC,又名.vue
文件)的两个新功能已脱离实验状态,现在是比较稳定的:
<script setup>
是一种编译时语法糖,可在 SFC 内使用 Composition API 时使用人体工程学来极大地改善。<style> v-bind
在 SFC<style>
标签中启用组件状态驱动的动态 CSS 值。
示例:
<script setup>
import { ref } from 'vue'
const color = ref('red')
</script>
<template>
<button @click="color = color === 'red' ? 'green' : 'red'">
Color is: {{ color }}
</button>
</template>
<style scoped>
button {
color: v-bind(color);
}
</style>
Web Components 网路组件
Vue 3.2 引入了defineCustomElement
,这是一种使用 Vue 组件 API轻松创建原生自定义元素的新方法:
import { defineCustomElement } from 'vue'
const MyVueElement = defineCustomElement({
// normal Vue component options here
})
// Register the custom element.
// After registration, all `<my-vue-element>` tags
// on the page will be upgraded.
customElements.define('my-vue-element', MyVueElement)
这个 API 允许开发者创建使用 Vue 驱动的 UI 组件库,这些库可以与任何框架一起使用,或者根本没有框架。
性能改进
进行了一些重大的性能改进,具体来说:
更高效的 ref 实现(约 260% 的读取速度/约 50% 的写入速度)
模板编译器也得到了一些改进:
最后,有一个新v-memo
指令提供了记忆模板树的一部分能力。
v-memo
是一行添加的,使 Vue 成为js-framework-benchmark 中最快的主流框架之一: