正确用法
- 在组件中使用(这里结合了 transition 内置动画组件 )
<template>
<div class="layout clearfix">
<router-view v-slot="{ Component, route }">
<transition name="fade-transform" mode="out-in" v-if="!route.meta.keepAlive">
<component :is="Component" :key="route.name" />
</transition>
<transition name="fade-transform" mode="out-in" v-if="route.meta.keepAlive">
<keep-alive>
<component :is="Component" :key="route.name"/>
</keep-alive>
</transition>
</router-view>
</div>
</template>
<script setup>
import { onActivated, onDeactivated } from 'vue'
onActivated(() => {
console.log('onActivated')
})
onDeactivated(() => {
console.log('onDeactivated')
})
</script>
- 在router.js中配置 keepAlive 自定义属性
import { createRouter, createWebHistory } from 'vue-router'
const routes = [
{
path: '/',
name: 'layout',
component: () => import('../layout/index.vue'),
children: [
{
path: '/home',
name: 'home',
component: () => import('../views/home.vue'),
meta{
title: '工作台',
keepAlive: true
}
},
]
},
{
path: '/login',
name: 'login',
component: () => import('../views/login.vue')
},
{
path: '/:pathMatch(.*)',
component: () => import('../views/404.vue')
}
]
const router = createRouter({
history: createWebHistory(process.env.BASE_URL),
routes
})
export default router
根据条件缓存页面
<template>
<div id="app">
<router-view v-slot="{ Component, route }">
<keep-alive>
<component :is="Component" :key="route.path"></component>
</keep-alive>
</router-view>
<router-view v-slot="{ Component, route }">
<keep-alive include='a,b'>
<component :is="Component" key="route.path"></component>
</keep-alive>
</router-view>
<router-view v-slot="{ Component, route }">
<keep-alive :include='/a|b/'>
<component :is="Component" key="route.path"></component>
</keep-alive>
</router-view>
<router-view v-slot="{ Component, route }">
<keep-alive include="['a', 'b']">
<component :is="Component" key="route.path"></component>
</keep-alive>
</router-view>
<router-view v-slot="{ Component, route }">
<keep-alive :max="10">
<component :is="Component" key="route.path"></component>
</keep-alive>
</router-view>
<router-view v-slot="{ Component, route }">
<keep-alive exclude='test'>
<component :is="Component" key="route.path"></component>
</keep-alive>
</router-view>
</div>
</template>
<script setup>
</script>
错误示例
VueCompilerError: expects exactly one child element or component.
<router-view v-slot="{ Component, route }">
<transition name="fade-transform" mode="out-in" >
<component :is="Component" :key="route.path" v-if="!route.meta.keepAlive"/>
<keep-alive>
<component :is="Component" :key="route.path" v-if="route.meta.keepAlive"/>
</keep-alive>
</transition>
</router-view>
不报错,但 keep-alive 内置组件的缓存没有效果,onActivated 函数也不会执行
<router-view v-slot="{ Component, route }">
<transition name="fade-transform" mode="out-in" >
<component :is="Component" :key="route.path" v-if="!route.meta.keepAlive"/>
</transition>
<keep-alive>
<transition name="fade-transform" mode="out-in" >
<component :is="Component" :key="route.path" v-if="route.meta.keepAlive"/>
</transition>
</keep-alive>
</router-view>
vue中keep-alive的使用及详解