vue通过iframe引入一个外链地址,怎么保证每次切换回这个已经打开的tab页的时候iframe不会重新加载

我有很多的报表页。这些页面是通过外链引入的。他们只有id是不一样的。

我的页面是这样写的。router定义成

复制代码
{
  path: '/report-template/:id',
  title: '统计模版',
  name: 'report-template',
  component: () =>
      import(/*webpackChunkName: "report-template"*/ '@/views/report-management/report-template.vue'),
},

report-template.vue

复制代码
<template>
  <div class="external-page-container">
    <iframe
        :src="srcUrl"
        frameborder="0"
        width="100%"
        allowfullscreen
    ></iframe>
  </div>
</template>

<script>
export default {
  name:'report-template',
  data() {
    return {
      id: 0,
      srcUrl: "",
    }
  },
  watch: {
    '$route.params' (to) {
      // 路由发生变化.iframe地址改变
     this.$nextTick(()=>{
       this.getIframe()
     })
    },
  },
  mounted(){
    this.getIframe()
  },
  methods:{
    getIframe(){
      this.id=this.$route.params.id;
      this.srcUrl=`https://jm/jmreport/view/${this.id}`; //拼接访问地址
    }
  }
}
</script>

<style scoped>
.external-page-container{
  width: 100%;
  height: 100%;
  position: relative;
}
iframe{
  position: absolute;
  top: 0;
  bottom: 0;
  height: 100%;
}
</style>

上面能实现点击不同的报表菜单。展示不同的报表数据。

但是发现按照上面的写法打开了一个报表菜单之后再点击别的。再次切换回这个报表的菜单。页面会刷新一下,重新加载这个iframe。如何做到打开之后就记住当时的状态,包含输入框查询的内容。

这时候我们可以把iframe打开过的id都保存起来,把所有的iframe都以v-show的形式显示和隐藏。并且注意最外层的要加上keep-alive。下面cachePage包含所有打开的路由页面

复制代码
<!-- 路由渲染的右侧所有数据的地方在这 包含着缓存数据 -->
<keep-alive :include="cachePage">
  <router-view></router-view>
</keep-alive>

然后优化这个report-template.vue

复制代码
<template>
  <div class="external-page-container">
    <div
      v-for="openedId in openedReportIds"
      :key="openedId"
      class="iframe-wrapper"
      v-show="currentId == openedId"
    >
      <!-- 使用缓存的 urlMap,打开后的url再次切换回去可以记住之前的筛选内容 -->
      <iframe
        :src="urlMap[openedId]"
        frameborder="0"
        width="100%"
        height="100%"
        allowfullscreen
      ></iframe>
    </div>
  </div>
</template>

<script>
export default {
  name: 'report-template',
  data() {
    return {
      currentId: null,
      openedReportIds: [],
      urlMap: {} // 缓存每个 ID 对应的完整 URL
    }
  },
  watch: {
    '$route.params.id': {
      handler(newId) {
        if (newId && newId !== this.currentId) {
          this.currentId = newId;
          if (!this.openedReportIds.includes(newId)) {
            this.openedReportIds.push(newId);
            // 只在首次打开时计算并缓存 URL
            this.urlMap[newId] = this.getIframeUrl(newId);
          }
        }
      },
      immediate: true
    }
  },
  methods: {
    getIframeUrl(id) {
      this.id=this.$route.params.id;         this.srcUrl=`https://jm/jmreport/view/${this.id}`; //拼接访问地址
    }
  }
}
</script>
<style scoped>
.external-page-container{
  width: 100%;
  height: 100%;
  position: relative;
}
iframe{
  position: absolute;
  top: 0;
  bottom: 0;
  height: 100%;
}
</style>
相关推荐
lbh1 小时前
当我开始像写代码一样和AI对话,一切都变了
前端·openai·ai编程
We་ct2 小时前
LeetCode 918. 环形子数组的最大和:两种解法详解
前端·数据结构·算法·leetcode·typescript·动态规划·取反
qq_406176142 小时前
深入浅出 Pinia:Vue3 时代的状态管理新选择
javascript·vue.js·ecmascript
wefly20173 小时前
m3u8live.cn 在线M3U8播放器,免安装高效验流排错
前端·后端·python·音视频·前端开发工具
C澒3 小时前
微前端容器标准化 —— 公共能力篇:通用打印
前端·架构
德育处主任Pro3 小时前
前端元素转图片,dom-to-image-more入门教程
前端·javascript·vue.js
木斯佳3 小时前
前端八股文面经大全:小红书前端一二面OC(下)·(2026-03-17)·面经深度解析
前端·vue3·proxy·八股·响应式
陈天伟教授4 小时前
人工智能应用- 预测新冠病毒传染性:04. 中国:强力措施遏制疫情
前端·人工智能·安全·xss·csrf
叫我一声阿雷吧4 小时前
JS 入门通关手册(23):JS 异步编程:回调函数与异步本质
javascript·es6·前端面试·回调函数·回调地狱·js异步编程·异步本质
zayzy4 小时前
前端八股总结
开发语言·前端·javascript