Vue3+setup语法糖:keepalive的使用和动态删除缓存

需求

项目需求:添加tab标签页功能,切换tab时保留它们的状态,关闭标签清除状态。

解决:用 <KeepAlive> 内置组件将这些动态组件包装起来

官方地址:cn.vuejs.org/guide/built...

使用

<KeepAlive> 默认会缓存内部的所有组件实例,但我们可以通过 includeexclude prop 来定制组件。

由于项目所需要保存状态页面较少,我这里选择了includeinclude的值可以是以英文逗号分隔的字符串、一个正则表达式,或是包含这两种类型的一个数组,这里便于后续动态删除使用数组。

!!!重点

include匹配的值是组件的name,如果发现使用include无效,八成是name不匹配。由于使用了setup语法糖,会自动生成组件name。

获取组件name:router.currentRoute.value.matched[0].components.default.__name

代码:

js 复制代码
//App.vue
<template>
  <div class="content" v-else>
    <LeftVue />
    <div class="rightVue">
      <HeadVue />
      <!--tab标签-->
      <PageVue /> 
      
            <!--main-->
        <router-view v-slot="{ Component }">
          <keep-alive :include="includeList">
            <component class="main" :is="Component" :key="$route.fullPath" />
          </keep-alive>
        </router-view>
    </div>
</template>

<script setup>
...
//包含缓存组件
const includeList = ref(["Index"]);

</script>

简单引入后,会发现组件名"Index"的页面会保存状态了。

动态删除缓存

思路:

1.在app.vue的includeList添加需要缓存组件名(如上)

2.当点击tab的关闭按钮时删除includeList中对应的值

3.从其他页面进入需要缓存的页面时,向includeList添加数据(防止需要缓存的页面,关闭后不会再缓存)

app.vue定义一个增删方法,tab标签点击关闭时调用方法(子组件调用父组件的方法)

js 复制代码
//App.vue
<template>
  <div class="content" v-else>
    <LeftVue />
    <div class="rightVue">
      <HeadVue />
      <!--tab标签-->
      <PageVue  @changeCaseKey="changeCaseKey" /> 
      
            <!--main-->
        <router-view v-slot="{ Component }">
          <keep-alive :include="includeList">
            <component class="main" :is="Component" :key="$route.fullPath" />
          </keep-alive>
        </router-view>
    </div>
</template>

<script setup>
...
//省略无关代码
// 包含缓存组件
const includeList = ref(["Index"]);

// keepalive缓存------重点
const changeCaseKey = (path,fixed) => {
  let val = path.default.__name;
  let index = includeList.value.findIndex((item) => item == val);
  if(fixed=='del'){
    console.log(index);
    index == -1 ? "" : includeList.value.splice(index, 1);
  }else if(fixed=='add'){
    index == -1 ? includeList.value.push(val):'';
  }
 
};
</script>
js 复制代码
//page.vue
<script setup>
...
//省略无关代码

// 接受父组件方法
const emits = defineEmits(["changeCaseKey"]);

//关闭tab
const delPage = (item, index) => {
  emits("changeCaseKey",router.currentRoute.value.matched[0].components,'del');
};
</script>
js 复制代码
//需要跳转增加的页面,使用
 emits("changeCaseKey",router.currentRoute.value.matched[0].components,'add');

大功告成!!

相关推荐
2301_768350232 小时前
Vue第二期:组件及组件化和组件的生命周期
前端·javascript·vue.js
华洛3 小时前
公开一个AI产品的商业逻辑与设计方案——AI带来的涂色卡自由
前端·后端·产品
明远湖之鱼3 小时前
opentype.js 使用与文字渲染
前端·svg·字体
90后的晨仔4 小时前
Vue 3 组合式函数(Composables)全面解析:从原理到实战
前端·vue.js
今天头发还在吗4 小时前
【React】动态SVG连接线实现:图片与按钮的可视化映射
前端·javascript·react.js·typescript·前端框架
小刘不知道叫啥4 小时前
React 源码揭秘 | suspense 和 unwind流程
前端·javascript·react.js
szial4 小时前
为什么 React 推荐 “不可变更新”:深入理解 React 的核心设计理念
前端·react.js·前端框架
mapbar_front4 小时前
面试是一门学问
前端·面试
90后的晨仔5 小时前
Vue 3 中 Provide / Inject 在异步时不起作用原因分析(二)?
前端·vue.js
90后的晨仔5 小时前
Vue 3 中 Provide / Inject 在异步时不起作用原因分析(一)?
前端·vue.js