设置keepAlive
// 配置代码
<route>
{
name: '设置',
meta: { keepAlive: true }
}
</route>
//
<script>
export default {
name: '设置', // 需要设置页面name 和 rotue 中的name 一致
data () {
return {
data: { }
}
},
// ...
// 实现逻辑代码
// /router/index.js
if (to.meta.keepAlive) {
const keepAlive = keepAliveStore()
keepAlive.add(to.name) // 将路由名称添加到 keepAlive 集合中
}
// /layout/index.vue
<router-view v-slot="{ Component, route }">
<keep-alive :include='keepAlive.list'>
<component :is="Component" :key="route.fullPath" />
</keep-alive>
</router-view>