【前端分享】vue3 有 keep-alive属性吗?

Vue2 和 Vue3 使用 Keep-alive 最全对比(含完整代码)

我给你用最清晰、最实用、直接可复制 的方式讲清楚:Vue2 怎么用、Vue3 怎么用、两者区别在哪。

1. 一、先记住核心结论

  1. Vue2 :<keep-alive> 全小写

  2. Vue3 :<KeepAlive> 大驼峰(兼容小写)

  3. 功能完全一致:缓存组件、保留状态、提升性能

  4. 使用场景完全一致:动态组件、路由页面


二、Vue2 中使用 keep-alive

2. 基础用法(动态组件)

复制代码
<!-- Vue2 -->
<template>
  <div>
    <keep-alive>
      <component :is="currentPage"></component>
    </keep-alive>
  </div>
</template>

3. 带条件缓存(include / exclude / max)

复制代码
<!-- 只缓存 name 为 Home、List 的组件 -->
<keep-alive include="Home,List" max="10">
  <component :is="currentPage"></component>
</keep-alive>
  • include:逗号分隔字符串 / 正则 / 数组

  • exclude:不缓存

  • max:最大缓存数(超出自动销毁最久未使用)

4. 路由页面缓存(最常用)

复制代码
<!-- Vue2 路由缓存 -->
<keep-alive>
  <router-view v-if="$route.meta.keepAlive" />
</keep-alive>

<router-view v-if="!$route.meta.keepAlive" />

路由配置:

复制代码
// router/index.js
{
  path: '/home',
  component: Home,
  meta: { keepAlive: true } // 缓存
}

5. Vue2 生命周期(缓存组件专用)

复制代码
export default {
  activated() {
    // 页面显示时触发(每次)
  },
  deactivated() {
    // 页面隐藏时触发
  }
}

三、Vue3 中使用 KeepAlive

6. 基础用法(动态组件)

复制代码
<!-- Vue3 推荐大驼峰 -->
<template>
  <KeepAlive>
    <component :is="currentPage" />
  </KeepAlive>
</template>

7. 条件缓存(和 Vue2 一样)

复制代码
<!-- 字符串形式 -->
<KeepAlive include="Home,List">
  <component :is="currentPage" />
</KeepAlive>

<!-- 数组形式(Vue3 更推荐) -->
<KeepAlive :include="['Home', 'List']" :max="10">
  <component :is="currentPage" />
</KeepAlive>

8. 路由页面缓存(Vue3 标准写法)

复制代码
<router-view v-slot="{ Component }">
  <KeepAlive>
    <component
      :is="Component"
      v-if="$route.meta.keepAlive"
    />
  </KeepAlive>
</router-view>

路由配置:

复制代码
{
  path: '/home',
  component: Home,
  meta: { keepAlive: true }
}

9. Vue3 组合式API 生命周期

复制代码
<script setup>
import { onActivated, onDeactivated } from 'vue'

// 每次显示组件
onActivated(() => {
  console.log('组件激活')
})

// 每次隐藏组件
onDeactivated(() => {
  console.log('组件失活')
})
</script>

四、Vue2 和 Vue3 使用 Keep-alive 核心区别

用法 Vue2 Vue3
组件名 <keep-alive> <KeepAlive> (大驼峰)
语法 选项式 API 组合式 API <script setup>
生命周期 activated / deactivated onActivated / onDeactivated
include 支持 字符串、正则 字符串、数组、正则(更强)
路由缓存写法 简单 v-if 标准 v-slot 写法
缓存底层 数组/对象 Map / Set(性能更高)

五、最关键的注意事项(必看!)

  1. 缓存依据是组件的 name

    没写 name 会缓存失败!

    • Vue2:export default { name: 'Home' }

    • Vue3:<script setup> 自动以文件名作为 name

  2. Keep-alive 不会销毁组件

    只会激活/失活,所以数据会保留

  3. 只对 动态组件 / 路由切换 生效

    普通组件写了没用

  4. max 一定要设置

    防止缓存太多导致内存溢出


六、一句话总结

  • Vue2 :<keep-alive> 小写,选项式 API

  • Vue3 :<KeepAlive> 大驼峰,组合式 API

  • 功能一模一样,只是写法和生命周期略有区别

  • 路由缓存、动态组件缓存是最常用场景

相关推荐
程序员老赵1 小时前
Docker 部署青龙面板:轻松搭建定时任务管理平台
前端·docker·开源
Swot1 小时前
Asciidoctor 公式数字下标错位问题:原因分析与解决方案
前端
Swot1 小时前
leo-editor 中将 Markdown 内容转成 asciidoc 格式
前端
狒狒AI1 小时前
Remotion / GSAP+Three / HyperFrames / Skia 四栈实测:代码渲染知识视频的选栈方法(附开源技能)
前端·人工智能·aigc
修炼的dance1 小时前
浏览器里导出H.265,8.5秒和55.5秒差在硬件
前端
补码缩写1 小时前
WebCodecs选编码器,探测之后还要验解码和播放
前端
大文说跨境1 小时前
亚马逊多账号管理浏览器怎么选?云登/紫鸟/AdsPower 对比
java·前端·自动化
hunter_0761 小时前
iOS 首页复杂嵌套滚动技术方案:头部固定 + 网格滚动 + Tab 吸顶 + 每 Tab 独立下拉刷新
前端
CoderYanger1 小时前
Java EE 进阶:4.Spring Web MVC 入门(第一弹)
java·开发语言·前端·spring·面试·java-ee·mvc
用户2788607285671 小时前
fallback 会盖掉平台提示词:system、空数组和 systemAppend 的差别
前端·后端