当前位置: 首页> 游戏> 评测 > 温州公司网址公司_网站建设哪家好采用苏州久远网络_百度关键词排名查询接口_软文300字案例

温州公司网址公司_网站建设哪家好采用苏州久远网络_百度关键词排名查询接口_软文300字案例

时间:2025/7/11 15:48:50来源:https://blog.csdn.net/jinxi1112/article/details/143355382 浏览次数:0次
温州公司网址公司_网站建设哪家好采用苏州久远网络_百度关键词排名查询接口_软文300字案例

app.vue(或其它入口页)使用keep-alive的include动态设置缓存

注意:有嵌套的页面需要在嵌套内部的router-view上添加keep-alive

<template><router-view v-slot="{ Component}"><keep-alive :include="cacheList"><component :is="Component" /></keep-alive></router-view>
</template>
<script setup lang="ts">
import { cacheStore } from "@/stores";
const store = cacheStore();
// 在store中定义一个cacheList用在存放缓存页面
const cacheList = computed(() => {return store.cacheList;
});
</script>

stores的index.ts:使用pinia调控缓存页面的数组

import { defineStore } from "pinia";
import { cacheState } from './types';
const cacheStore = defineStore("cache", {state: (): cacheState => ({cacheList:[] // keep-alive缓存列表}),actions: {pushCaches(item:string) {let set = new Set(this.cacheList);set.add(item);this.cacheList = Array.from(set);},popCaches(item:string) {let set = new Set(this.cacheList);if (set.has(item)) {set.delete(item);}this.cacheList = Array.from(set);},},
});
export default cacheStore;

list.vue(列表页)

注意:vue3的setup模式使用defineOptions为页面路由命名

<script lang="ts" setup>
import { ref } from "vue";
import { onBeforeRouteLeave} from "vue-router";
import { cacheStore } from "@/stores";
const store = cacheStore();
//路由命名(非组件名)
defineOptions({ name: "list" });
//离开页面守卫
onBeforeRouteLeave((to, _from, next) => {// 若跳转的是详情页缓存本页面,否则清除本页缓存if (to.name === "detail") {store.pushCaches("list");} else {store.popCaches("list");}next();
});
</script>

detail.vue(详情页)

<script lang="ts" setup>
//路由命名
defineOptions({ name: "detail" });
</script>

关键字:温州公司网址公司_网站建设哪家好采用苏州久远网络_百度关键词排名查询接口_软文300字案例

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: