1. App.vue
<template>
<div>
<keep-alive>
<router-view v-if="$route.meta.keepAlive"></router-view>
</keep-alive>
<router-view v-if="!$route.meta.keepAlive"></router-view>
</div>
</template>
2. router.js 中给需要缓存页面加上meta
{
path: "/info",
component: () => import("@/views/info"),
meta: {
keepAlive: true, // 需要缓存
},
},
3. 缓存的页面跳转路由时触发不了create钩子,可以使用$route()代替
watch: {
$route() {
// 进行需要的操作
},
},