vue3相比于vue2变化挺大的,在vue2中使用的时候,keep-alive是在router-view外层的。
如下:
<template>
<router-view v-slot="{ Component }">
<keep-alive>
<component :is="Component"></component>
</keep-alive>
</router-view>
</template>
<script setup lang="ts"></script>
部分组件缓存:
<template>
<router-view v-slot="{ Component }">
// Home、My是name属性名
<keep-alive :include="['Home', 'My']">
<component :is="Component"></component>
</keep-alive>
</router-view>
</template>
<script setup lang="ts"></script>