Vue iframe嵌套的页面实现路由缓存 实现keep-alive效果

本地页面用keep-alive路由缓存显示,iframe的页面用v-show显示

复制代码
<template>
  <section class="app-main">
    <component
      :key="item.name"
      :style="{ height: height, minHeight: height }"
      v-show="item.meta.externalUrl.indexOf($route.path) > -1"
      v-for="item in hasOpenComponentsArr"
      :is="item.component"
    >
    </component>
    <keep-alive :include="cachedViews">
      <router-view
        :key="key"
        :style="{ height: height, minHeight: height }"
        v-show="handleShow(key)"
      />
    </keep-alive>
  </section>
</template>

<script>
import { mapGetters } from "vuex";
import Vue from "vue/dist/vue.js";
export default {
  name: "AppMain",
  created() {
    // 设置iframe页的数组对象
    const componentsArr = this.getComponentsArr();
    componentsArr.forEach((item) => {
      Vue.component(item.name, item.component);
    });
    this.componentsArr = componentsArr;
    // 判断当前路由是否iframe页
    this.isOpenIframePage();
    // this.isNowFunc();
    this.$bus.$on("closePage", () => {
      this.isOpenIframePage();
    });
  },
  data() {
    return {
      height: "calc(100vh - 93px)",
      componentsArr: [], // 含有iframe的页面
    };
  },
  computed: {
    ...mapGetters(["sidebarRouters"]),
    cachedViews() {
      return this.$store.state.tagsView.cachedViews;
    },
    key() {
      return this.$route.path;
    },
    // 实现懒加载,只渲染已经打开过(hasOpen:true)的iframe页
    hasOpenComponentsArr() {
      return this.componentsArr.filter((item) => item.hasOpen);
    },
    visitedViews() {
      return this.$store.state.tagsView.visitedViews;
    },
  },
  watch: {
    $route: {
      handler(val) {
        this.isOpenIframePage();
      },
      immediate: true,
    },
  },
  methods: {
    // 不渲染包含iframe的路由
    handleShow(key) {
      var flag = true;
      var index = this.componentsArr.findIndex(
        (v) => v.meta.externalUrl.indexOf(key) > -1
      );
      if (index != -1) {
        flag = false;
      }
      return flag;
    },
    // 根据当前路由设置hasOpen
    isOpenIframePage() {
      var index = this.componentsArr.findIndex((item) => {
        return item.meta.externalUrl.indexOf(this.$route.path) > -1;
      });
      if (index != -1) {
        this.componentsArr[index].hasOpen = true;
      }
      if (this.visitedViews.length > 0) {
        this.componentsArr.forEach((item, itemIndex) => {
          var index = this.visitedViews.findIndex(
            (v) => item.meta.externalUrl.indexOf(v.fullPath) > -1
          );
          if (index == -1) {
            this.componentsArr[itemIndex].hasOpen = false;
          }
        });
      }
    },
    // 遍历路由的所有页面,把含有iframeComponent标识的收集起来
    getComponentsArr() {
      const routes = this.sidebarRouters;
      const iframeArr = [];
      routes.filter((items) => {
        if (items.children) {
          items.children.forEach((item) => {
            if (item.meta && item.meta.externalUrl) {
              iframeArr.push({
                ...item,
                hasOpen: false,
                component: () => import("@/views/iframe/index"),
              });
            }
          });
        }
      });
      return iframeArr;
    },
  },
};
</script>

<style lang="scss" scoped>
.app-main {
  overflow: auto;
  width: 100%;
  position: relative;
  overflow: hidden;
}
</style>
相关推荐
2501_940041749 分钟前
前端工程化进阶:5个高交互与可视化项目提示词
前端
你很易烊千玺10 分钟前
JS 异步 从零讲(大白话 + 真实场景 + 可运行案例)
前端·javascript·vue.js
月落归舟32 分钟前
MyBatis缓存机制
java·缓存·mybatis
CAE虚拟与现实2 小时前
Redis如何保证存和读的过程中数据的一致性?
数据库·redis·缓存
why技术2 小时前
AI Coding开始进入第四个时代,我还没上车呢!
前端·人工智能·后端
大家的林语冰3 小时前
CSS 已死?DOM 性能黑洞!Pretext 排版革命让你在文本间跳舞,没有 DOM 也能纵享丝滑~
前端·javascript·css
vipbic3 小时前
我也该升级了,陪伴了我7年的博客
前端
Lee川3 小时前
RAG 实战:从一篇掘金文章出发,拆解检索增强生成的全链路
前端·人工智能·后端
Lee川4 小时前
MCP 高德地图实战:当 AI 学会使用工具,一个协议如何重塑大模型的行动边界
前端·人工智能·后端
ZC跨境爬虫4 小时前
跟着 MDN 学CSS day_14:(尺寸调整技能测试与实战解析)
前端·css·ui·html·tensorflow