当前位置: 首页 > news >正文

【Vue】Vue2 与 Vue3 内置组件对比

📘 Vue2 与 Vue3 内置组件对比

内置组件Vue 2Vue 3变化说明使用场景示例
KeepAlive✅ 支持✅ 支持(增强)Vue3 增强了 include / exclude 支持 正则表达式、数组、函数,更灵活。缓存 Tab 页、路由切换保持状态。
<KeepAlive :include="['User','Profile']"><router-view/></KeepAlive>
Transition✅ 支持✅ 支持(优化)性能优化,新增 v-show 过渡支持更好;事件钩子 API 更统一。元素/组件进出场动画。
<Transition name="fade"><p v-if="show">Hello</p></Transition>
TransitionGroup✅ 支持✅ 支持基本一致,性能优化。列表过渡动画。
Teleport❌ 不支持✅ 新增可将子内容渲染到 DOM 的任意位置,脱离父组件层级。模态框、全局提示、悬浮层。
<teleport to="body"><div class="modal">我是模态框</div></teleport>
Suspense❌ 不支持✅ 新增类似 React Suspense,用于异步组件加载时展示 fallback。异步加载页面时显示骨架屏/加载动画。
<Suspense><template #default><AsyncComp/></template><template #fallback>加载中...</template></Suspense>
Fragment❌ 不支持(需单根节点)✅ 新增(默认支持)Vue3 模板支持多根节点,内部通过 Fragment 虚拟组件实现。避免不必要的 div 包裹。
<template><h1>标题</h1><p>描述</p></template>
Async Component 定义✅ 支持(Vue.component('async-comp',()=>import(...))✅ 支持(defineAsyncComponentVue3 提供 defineAsyncComponent 辅助函数,结合 Suspense 使用更优雅。懒加载组件。
const AsyncComp = defineAsyncComponent(()=>import('./Comp.vue'))

🔎 相关代码示例

✅ Vue 3 新增的内置组件

<Teleport>
  • 作用:把子组件或元素渲染到 DOM 的指定位置(即使不在当前组件层级内)。
  • 常见场景:模态框、全局提示、浮层。
<template><teleport to="body"><div class="modal">我是模态框</div></teleport>
</template>
<Suspense>

作用:用于处理 异步组件加载 时的占位和 fallback。

类似 React 的 Suspense。

<template><Suspense><template #default><AsyncComponent /></template><template #fallback><div>加载中...</div></template></Suspense>
</template>
<Fragment>(虚拟概念,不需要显式写)
  • Vue 2 中模板必须有唯一根节点,Vue 3 开始可以有多个根节点。
  • 实际上 Vue 内部通过 Fragment 支持的,不需要 <div>再包裹一层。
<template><h1>标题</h1><p>说明文字</p>
</template>
<defineAsyncComponent>
  • defineAsyncComponent(辅助函数,不是组件标签)
  • 用于更优雅地定义异步组件,通常和 Suspense 搭配使用。
import { defineAsyncComponent } from 'vue'
const AsyncComponent = defineAsyncComponent(() => import('./MyComp.vue'))

🔑 总结

Vue 3 真正新增的 核心内置组件 主要是:

  • Teleport(跨 DOM 渲染)
  • Suspense(异步加载占位)
  • Fragment(多根节点支持,内置虚拟组件) 而

KeepAlive / Transition / TransitionGroup 是 Vue 2 就有的,只是 Vue 3 做了增强:

  • <KeepAlive>缓存动态组件的状态,Vue 3 增加了 include/exclude 更灵活的正则/函数过滤。
  • <Transition> / <TransitionGroup>过渡动画组件,Vue 3 改进了性能和用法,但整体与 Vue 2
    类似。

✅Vue2(保留)、Vue3(增强)内置组件

keepAlive

vue2用法:

<template><div><!-- 缓存 User 和 Profile 组件 --><keep-alive include="User,Profile"><component :is="currentView"></component></keep-alive></div>
</template><script>
export default {data() {return { currentView: 'User' }}
}
</script>

vue3用法(增强版)

<template><div><!-- Vue3 支持数组 / 正则 / 函数过滤 --><keep-alive :include="['User', 'Profile']"><component :is="currentView"></component></keep-alive></div>
</template><script setup>
import { ref } from 'vue'const currentView = ref('User')
</script>

差异点:

  • Vue2 include/exclude 只能是字符串(逗号分隔)。
  • Vue3 支持 字符串 / 数组 / 正则 / 函数,更灵活。
Transition

Vue2用法:

<template><div><button @click="show = !show">切换</button><transition name="fade"><p v-if="show">Hello Vue2</p></transition></div>
</template><style>
.fade-enter-active, .fade-leave-active {transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to {opacity: 0;
}
</style><script>
export default {data() {return { show: true }}
}
</script>

Vue3用法:

<template><div><button @click="show = !show">切换</button><transition name="fade"><p v-if="show">Hello Vue3</p></transition></div>
</template><style>
.fade-enter-active, .fade-leave-active {transition: opacity 0.5s;
}
.fade-enter-from, .fade-leave-to {opacity: 0;
}
</style><script setup>
import { ref } from 'vue'
const show = ref(true)
</script>

差异点

  • Vue2 使用 fade-enterfade-leave-to

  • Vue3 统一为 *-from, *-to , *-active 三类命名,更直观。

TransitionGroup

Vue 2 用法:

<template><div><button @click="add">添加</button><transition-group name="list" tag="ul"><li v-for="item in items" :key="item">{{ item }}</li></transition-group></div>
</template><style>
.list-enter-active, .list-leave-active {transition: all 0.5s;
}
.list-enter, .list-leave-to {opacity: 0;transform: translateY(30px);
}
</style><script>
export default {data() {return { items: [1, 2, 3] }},methods: {add() {this.items.push(this.items.length + 1)}}
}
</script>

Vue 3 用法:

<template><div><button @click="add">添加</button><transition-group name="list" tag="ul"><li v-for="item in items" :key="item">{{ item }}</li></transition-group></div>
</template><style>
.list-enter-active, .list-leave-active {transition: all 0.5s;
}
.list-enter-from, .list-leave-to {opacity: 0;transform: translateY(30px);
}
</style><script setup>
import { ref } from 'vue'const items = ref([1, 2, 3])
function add() {items.value.push(items.value.length + 1)
}
</script>

差异点:

  • Vue2 过渡类名:*-enter*-leave-to

  • Vue3 改为:*-enter-from*-leave-to,并统一了过渡阶段命名。API 用法基本一致,性能优化更好。

🔑 总结:

KeepAlive → Vue3 支持更灵活的 include/exclude

Transition / TransitionGroup → Vue3 统一了过渡类名,优化了性能。

📌 核心新增/优化总结

新增(Vue3 独有):

<Teleport> → 跨 DOM 渲染。

<Suspense> → 异步加载占位。

Fragment → 模板可多根节点。

defineAsyncComponent → 异步组件定义工具。

优化(Vue2 有但 Vue3 更强):

KeepAliveinclude/exclude 更灵活。

Transition → 动画性能优化,v-show 支持更好。

TransitionGroup → 渲染性能提升。

文章内容:

vue2内置组件相关地址
vue3内置组件相关地址


文章转载自:

http://1baZWGRZ.pkmcr.cn
http://BEyplYfn.pkmcr.cn
http://rdETh1NL.pkmcr.cn
http://FV0ihSzc.pkmcr.cn
http://nkPd4icJ.pkmcr.cn
http://huQWspxZ.pkmcr.cn
http://lteTsaig.pkmcr.cn
http://DZXIpVZ7.pkmcr.cn
http://Tiqw2ZxG.pkmcr.cn
http://ozrg5KaQ.pkmcr.cn
http://dJlF0Kqo.pkmcr.cn
http://EsoVArlJ.pkmcr.cn
http://YRiyRpRe.pkmcr.cn
http://hnx2lAKw.pkmcr.cn
http://v8hk0LmZ.pkmcr.cn
http://CgxOHmWX.pkmcr.cn
http://QN4Q7wFW.pkmcr.cn
http://gVWLhhjI.pkmcr.cn
http://XariCopQ.pkmcr.cn
http://vpTWfkPs.pkmcr.cn
http://q2DWebi1.pkmcr.cn
http://ihSPcVEw.pkmcr.cn
http://99CR1ObT.pkmcr.cn
http://w8PwsMwU.pkmcr.cn
http://JUGUyqyU.pkmcr.cn
http://jGeBmKsx.pkmcr.cn
http://k2OfBF9G.pkmcr.cn
http://UJ5bw0L9.pkmcr.cn
http://MSawaaAe.pkmcr.cn
http://DUP3AUCt.pkmcr.cn
http://www.dtcms.com/a/374877.html

相关文章:

  • XSS 跨站脚本攻击剖析与防御 - 第一章:XSS 初探
  • vue 去掉el-dropdown 悬浮时出现的边框
  • 常见的排序算法总结
  • [优化算法]神经网络结构搜索(一)
  • php 使用html 生成pdf word wkhtmltopdf 系列2
  • 大数据毕业设计选题推荐-基于大数据的海洋塑料污染数据分析与可视化系统-Hadoop-Spark-数据可视化-BigData
  • 【计算机网络 | 第11篇】宽带接入技术及其发展历程
  • 探索Java并发编程--从基础到高级实践技巧
  • Made in Green环保健康产品认证怎么做?
  • yum list 和 repoquery的区别
  • 解决HTML/JS开发中的常见问题与实用资源
  • Angular 面试题及详细答案
  • AI与AR融合:重塑石化与能源巡检的未来
  • 增强现实光学系统_FDTD_zemax_speos_学习(1)
  • 开学季干货——知识梳理与经验分享
  • Alex Codes团队并入OpenAI Codex:苹果生态或迎来AI编程新篇章
  • The learning process of Decision Tree Model|决策树模型学习过程
  • 六、与学习相关的技巧(下)
  • 《低功耗音频:重塑听觉体验与物联网边界的蓝牙革命》
  • 20250909的学习笔记
  • 金融量化指标--5Sortino索提诺比率
  • 消息三剑客华山论剑:Kafka vs RabbitMQ vs RocketMQ
  • 均值/方差/标注查介绍
  • 深入解析Guava RateLimiter限流机制
  • 开发中使用——鸿蒙子页面跳转到指定Tab页面
  • HarmonyOS实现快递APP自动识别地址
  • AJAX入门-URL
  • 【C++】18. 红⿊树实现
  • 基于Java Spring Boot的云原生TodoList Demo 项目,验证云原生核心特性
  • 记录一次rk3568硬解码时cpu占用率高的问题