vue3.5新特性

embedded/2024/9/21 12:42:28/

vue在2024.09.03发布了3.5正式版本,其中包含多方面的升级和优化

性能优化

响应式系统重构优化,在内存占用、性能方面均有收益

Memory Usage Improvements

Given a test case with 1000 refs + 2000 computeds (1000 chained pairs) + 1000 effects subscribing to the last computed, comparing the total memory used by these class instances:

  • Before (3.4.19): 1426k
  • After (this PR): 631k (-56%)

Performance Comparison

Reasonable gains across the board, most notably for single ref invoking multiple effects (+118%~185%) and reading multiple invalidated computeds (+176%~244%).

  • 单个 ref 调用多个 effects:当一个 ref(Vue 中的响应式数据)被多次使用并触发多个副作用(effects)时,性能提升了118%到185%。这意味着在 Vue 的新优化下,相同的操作现在比以前快了将近两倍。
  • 读取多个无效的计算属性:计算属性是基于响应式数据的缓存值。当这些计算属性由于依赖的数据改变而变得无效时,重新读取这些属性的性能提升了176%到244%。这表明在处理多次读取失效的计算属性时,性能有了非常明显的提升

响应式相关

defnieProps支持解构

  • 之前defineProps不支持解构,相比之前,支持解构后,不失响应式特性
const { count } = defineProps(['count'])

watch onWatcherCleanup支持注册清理函数

  • Watch api中,监听对象发生变化后,清理副作用不再使用的引用,避免内存泄漏
watch(id, async (newId, oldId) => {const { response, cancel } = doAsyncWork(newId)// `cancel` will be called if `id` changes, cancelling// the previous request if it hasn't completed yetonWatcherCleanup(cancel)data.value = await response
})
function doAsyncWork(id: string) {const source = new AbortController();const { signal } = source;const response = fetch(`/api/data?id=${id}`, { signal });return { response, cancel: () => source.abort() };
}
  • 和onEffectCleanup的区别,不在watch中用,就不会被注册生效
<script setup>
watchEffect(async () => {data1.value = await queryAsyncData('api1', id.value)data2.value = await queryAsyncData('api2', id.value)
})// When getting called outside of the scope of Watch API,
// since there is no active effect scope,
// therefore the cleanup callback for cancel will not be registered
async function pull() {data3.value = await queryAsyncData('api3', id.value)
}
pull()
</script>

watch deep支持数字

// infinite depth
watch(src, () => {}, { deep: true })// explicit depth
watch(src, () => {}, { deep: 1 })

新增pause/resume方法 控制effect执行

  • 主要是为了避免一些不必要的副作用执行以提升性能,比如keep-alive组件,暂停deactive非激活状态下的副作用执行,或者结合IntersectionObserver延迟执行副作用场景
const { stop, resume, pause } = watch(refs,callback)stop()resume()pause()

onEffectCleanup支持注册清理函数

  • 利用副作用,清除监听、定时器等不再使用的引用,避免内存泄漏
export function queryAsyncData(api, id) {const { response, cancel } = doAsyncWork(api, id)// `cancel` will be called if this function get called in Watch API and the effect source changed// so that previous pending request will be cancelled// if not yet completedif (getCurrentEffect()) {onEffectCleanup(cancel)}return response
}
// in component setup or vapor render
watchEffect(() => {on(el1, eventName.value, handler)on(el2, eventName.value, handler)
})// in runtime-vapor pkg
function on(el, event, handler) {el.addEventListener(event, handler)if (getCurrentEffect()) {onEffectCleanup(() => {el.removeEventListener(event, handler)})}
}

add failSilently argument for onScopeDispose

  • onScopeDispose 用于在组件销毁时自动清除定时器,避免内存泄漏
import { ref, onScopeDispose } from 'vue';export default {setup() {const timer = ref(null);function startTimer() {timer.value = setInterval(() => {console.log('Timer is running');}, 1000);}// 当作用域销毁时,清除定时器onScopeDispose(() => {clearInterval(timer.value);},true);startTimer();return {timer,};},
};
  • 新增参数,支持静默告警
    • 其实就是提供了屏蔽告警日志的参数

其他新特性

useId

  • 主要是为了创建唯一的id用在dom属性上,每次调用 useId()都会创建唯一的id
<script setup>
import { useId } from 'vue'const id = useId()
</script><template><form><label :for="id">Name:</label><input :id="id" type="text" /></form>
</template>

useTemplateRef

  • 以往用ref既是用于响应式数据的,又用在dom上,就会会造成疑惑,所以专门有这个处理dom
<script setup>
import { useTemplateRef, onMounted } from 'vue'const inputRef = useTemplateRef('input')onMounted(() => {inputRef.value.focus()
})
</script><template><input ref="input" />
</template>
app.onUnmount()
  • 卸载时一些处理,比如清理不必要的引用等
function install(app: App) {function cleanupSomeSideeffect() {/* ...*/}//清理app.onUnmount(cleanupSomeSideffect);
}app.config.throwUnhandledErrorInProduction
- 主要为了生产环境下抛出错误
app.config.throwUnhandledErrorInProduction = true

自定义元素

  • Vue对Web Components的支持,使得开发者可以创建自定义元素,这些元素可以跨框架使用,甚至在不使用Vue.js的项目中也可以使用。

http://www.ppmy.cn/embedded/114588.html

相关文章

markdown-it:将Markdown文本转换为HTML格式,展示在页面,怎么自定义里面的a标签设置为在新标签页打开

由markdown-it将文本生成html然后渲染到页面上&#xff0c;但是现在你点击里面生成好的链接只能在本标签页打开&#xff0c;怎么将其设置为在新标签打开呢&#xff1f; 安装markdown-it npm install markdown-it 使用markdown-it const mdi new MarkdownIt({html: true,l…

油烟机制造5G智能工厂物联数字孪生平台,推进制造业数字化转型

油烟机制造5G智能工厂物联数字孪生平台&#xff0c;是智能制造与信息技术的深度融合产物。数字孪生工业互联平台通过部署在工厂各个环节的传感器和设备&#xff0c;实时采集、分析和处理生产过程中的海量数据&#xff0c;构建出高度逼真的数字孪生模型。这一模型不仅能够真实反…

在 Mac 中设置环境变量

目录 什么是环境变量&#xff0c;为什么它们重要&#xff1f;什么是环境变量&#xff1f;举个例子 如何查看环境变量如何设置和修改环境变量1. 临时设置环境变量2. 永久设置环境变量3. 修改现有环境变量 环境变量在开发中的应用在 Node.js 项目中使用环境变量在 Python 项目中使…

前端页面访问url完整过程解析

前端页面访问URL的完整过程是一个复杂但有序的流程&#xff0c;它涉及到多个层面的技术和协议。以下是该过程的详细讲解&#xff1a; 1. 用户输入URL并按下回车 当用户在浏览器的地址栏中输入一个URL并按下回车时&#xff0c;浏览器开始处理这个请求。 2. 浏览器查找缓存 浏…

基于MTL的多任务视频推荐系统

多任务学习&#xff0c;也就是MTL(Multi-task Learning)&#xff0c;现在已经被用在很多领域了&#xff0c;比如处理自然语言、搞计算机视觉&#xff0c;还有语音识别这些领域。MTL在大规模的推荐系统里也玩得挺溜&#xff0c;尤其是那些做视频推荐的大家伙。 MTL的玩法就是&a…

vue3 自定义el-tree树形结构样式

这里样式设置主要用到了 windcss 实现效果 模拟数据 这里也可以用模拟的数据,下面用的是后端请求的真实数据 [{"id": 5,"rule_id": 0,"status": 1,"create_time": "2019-08-11 13:36:09","update_time": "…

AI免费UI页面生成

https://v0.dev/chat v0 - UI设计 cursor - 编写代码 参考&#xff1a;https://www.youtube.com/watch?vIyIVvAu1KZ4 界面和claude类似&#xff0c;右侧展示效果和代码 https://pagen.so/

科技与艺术完美融合的LED异形创意圆形(饼/盘)显示屏横空出世

随着LED技术的飞速发展&#xff0c;这款集科技与艺术于一体的异形创意圆形&#xff08;饼/盘&#xff09;显示屏&#xff0c;不仅以其独特的形态打破了传统显示屏的界限&#xff0c;更在视觉呈现上开启了前所未有的新篇章。它不再仅仅是信息传递的载体&#xff0c;而是成为了空…