首页 > 其他分享 >页面缓存

页面缓存

时间:2023-12-19 19:46:50浏览次数:24  
标签:缓存 name meta router import 页面

页面缓存根据需求分为以下3类

  • 类小程序:根据用户的跳转方法来决定如何操作页面缓存堆栈
  • 历史记录:根据用户访问的历史记录缓存一定数量的页面,超过限定数量时采用新进先出
  • 面包屑:根据面包屑结构,缓存当前页面的祖先页面

类小程序

  • 需要解决的问题
    • 实现以下常用跳转方法
      • navigateTo 保留当前页面,跳转到应用内的某个页面
      • redirectTo 关闭当前页面,跳转到应用内的某个页面
      • reLaunch 关闭所有页面,打开到应用内的某个页面
      • navigateBack 关闭当前页面,返回上一页面或多级页面
    • 以上跳转方式如何在 router-link 中实现
    • 需要拦截 history、router 下对应的方法
    • 需要拦截浏览器返回按钮

历史记录

面包屑

  • 以下例子实现了对 HomeView 这个父页面的组件的缓存
  • keep-alive 和 router-view 配合使用实现父级页面缓存
<template>
  <div>
    <nav>
      <router-link to="/">Home</router-link> |
      <router-link to="/about">About</router-link> |
      <router-link to="/about2">About2</router-link>
    </nav>
    <keep-alive :include="keepAliveInclude">
      <!-- keep-alive 以第一个 slot 的 key 为钥匙寻找对应的组件实例的,:key="$route.fullPath"用以缓存不同路由的同一组件 -->
      <router-view name="default" :key="$route.fullPath" />
    </keep-alive>
  </div>
</template>

<script lang="ts">
import { keepAliveInclude } from "@/utils/pageKeepAliveInclude";
import { defineComponent } from "vue";

export default defineComponent({
  setup() {
    return {
      // 需要缓存的组件名称,包含父页面路由信息中 component 或 components 中所有组件的名称
      keepAliveInclude,
    };
  },
});
</script>
  • 路由
import Vue from "vue";
import VueRouter, { RouteConfig } from "vue-router";
import HomeView from "../views/HomeView.vue";
import { setKeepAliveInclude } from "@/utils/pageKeepAliveInclude";
import HeadAndSideVue from "@/components/HeadAndSide.vue";

Vue.use(VueRouter);

const routes: Array<RouteConfig> = [
  {
    path: "/",
    component: HeadAndSideVue,
    meta: {
      // 这个组件不需要缓存,不会被包含到 keepAliveInclude 中
      noKeepAlive: true,
    },
    children: [
      {
        path: "",
        name: "home",
        component: HomeView,
      },
      {
        path: "about",
        name: "about",
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/AboutView.vue"),
        meta: {
          // 当前页面的父级页面的 name
          fatherPageName: "home",
        },
      },
      {
        path: "about2",
        name: "about2",
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/AboutView.vue"),
        meta: {
          fatherPageName: "home",
        },
      },
    ],
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});
// 在路由拦截器中设置 keep-alive 的 include 绑定值
router.afterEach(setKeepAliveInclude);

export default router;
  • setKeepAliveInclude 和 keepAliveInclude
import router from "@/router";
import { Component, Ref, nextTick, ref } from "vue";
import { Route } from "vue-router";

export const keepAliveInclude: Ref<string[]> = ref([]);

function arrAdd<T>(arr: T[], item: T) {
  if (!arr.includes(item)) arr.push(item);
}
function _setKeepAliveInclude(route: Route) {
  if (!route.matched.length) return;
  route.matched.forEach((matched) => {
    if (matched.meta?.noKeepAlive) return;
    Object.values(matched.components).forEach((component) => {
      const name = (component as Component).name;
      if (name) {
        arrAdd(keepAliveInclude.value, name);
      } else {
        console.error(component);
        throw new Error(
          "路由中需要缓存(未配置meta.noKeepAlive)的组件,其组件名是必须的。以上组件缺少组件名:"
        );
      }
    });
  });
  if (route.meta?.fatherPageName) {
    const { route: fatherRoute } = router.resolve({
      name: route.meta.fatherPageName,
    });
    _setKeepAliveInclude(fatherRoute);
  }
}
export function setKeepAliveInclude(to: Route) {
  // 从子页面跳转到父页面时,等待 keepAlive 根据 include 包含的父组件名称获取缓存的实例
  // 然后重新生成 include,才把父页面的组件名称从 include 中删除,从而清除父页面的缓存
  nextTick(() => {
    keepAliveInclude.value.length = 0;
    // 根据面包屑层级,递归把父页面使用到的组件添加到include中
    if (to.meta?.fatherPageName) {
      const { route: fatherRoute } = router.resolve({
        name: to.meta.fatherPageName,
      });
      _setKeepAliveInclude(fatherRoute);
    }
  });
}

标签:缓存,name,meta,router,import,页面
From: https://www.cnblogs.com/qq3279338858/p/17914516.html

相关文章

  • 做一个wiki页面是体验HTML语义的好方法
    HTML语义:如何运用语义类标签来呈现Wiki网页在上一篇文章中,我花了大量的篇幅和你解释了正确使用语义类标签的好处和一些场景。那么,哪些场景适合用到语义类标签呢,又如何运用语义类标签呢?不知道你还记不记得在大学时代,你被导师逼着改毕业论文格式的情景,如果你回想一下,你在论文中使......
  • 第二章:SpringMVC的配置文件(web.xml)及访问页面
    一、开发环境二、创建maven工程三、默认方式配置web.xml四、扩展方式配置web.xml五、创建控制器六、配置springMVC配置文件七、访问首页八、访问指定页面九、总结......
  • 验证码组件开发,本地缓存倒计时
    vue组件<template><divclass="verifyCodemt-24"><divclass="header"><spanclass="logoinline-blockmr-6w-[26px]h-[26px]bg-black-0"></span><spanclass=&......
  • js获取当前页面网址
    在JavaScript中,可以使用window.location对象来获取当前页面的网址。以下是几种不同的实现方法:方法一:使用window.location.href varurl=window.location.href;console.log(url);结果:http://localhost/gridsys/Product/PlantQR?puid=8方法二:使用window.location.toStrin......
  • 【前端】做一个展示卡片样式数据的页面,解决卡片中图片高度不一致问题
    需求分析现在有这样一个需求,在一个页面展示数据,页面的数据通过卡片的形式展示,卡片中内容主要分为三部分,最上面个标题,中间是个图片,最下面是一排操作按钮。这里的卡片可以保证固定的宽高比例卡片可以随着窗口大小的变化做到自适应大小变化实际图片的高度和宽度不一致,显示的的时候可以......
  • 【UniApp】-uni-app-数据缓存
    前言好,经过上个章节的介绍完毕之后,给大家补充了一下uni-app-数据传递的内容那么补充了uni-app-数据传递的内容之后,这篇文章来给大家介绍一下uni-app-数据缓存搭建项目首先我们还是要先搭建一个项目,这里我就不多说了,大家可以参考上一篇文章搭建好项目之后,我们就可以开......
  • 使用Servlet进行页面跳转的两种方式
    最近在教学生学习JavaWeb相关的技术,刚好讲到Java当中的Servlet,一个服务端的小程序。也在和学生讲使用Servlet如何进行页面跳转,一种方式是使用请求转发进行页面跳转,一种方式是使用响应重定向。下面就来分别聊一聊这两种页面跳转方式。 首先有一个知识点需要注意,如果页......
  • 使用JS脚本修改页面内容
    在学习RPA时,我对使用JS脚本修改页面内容进行了深入学习。记录如下:获取元素的方法:1.document.getElementById("xxx")2.document.getElementsByClassName("xxx")3.document.getElementsByTagName("xxx")4.document.getElementsByName("xxx")5.document.querySele......
  • 14.adb 命令清缓存
    adbshell应用1查看目录结构:adbshellls查看系统当前日期:adbshelldate查看系统CPU使用情况:adbshellcat/proc/cpuinfo查看系统内存使用情况:adbshellcat/proc/meminfoadbshell应用2-查看应用列表显示所有应用:adbshellpmlistpackages显示系统自带应......
  • freemarker模板数据解析1--页面语法直接获取
    freemarker模板数据解析1--页面语法直接获取​ 对后端接口传递过来的Map<String,List>类型数据的展示。对于普通Map<String,String>数据,我们很清楚,直接上代码:·接口提供数据:Map<String,String>map=newHashMap<>();map.put("k1","v1");map.put("k2","v2"......