当前位置:首页 > VUE

vue iframe 实现tab

2026-01-17 00:17:55VUE

使用 Vue 和 iframe 实现 Tab 功能

在 Vue 中通过 iframe 实现 Tab 功能,可以结合动态组件或路由来实现多标签页的切换效果。以下是具体实现方法:

动态 iframe 切换

通过 v-if 或动态组件控制不同 iframe 的显示与隐藏,结合 Tab 标题实现切换逻辑。

<template>
  <div>
    <div class="tabs">
      <button 
        v-for="(tab, index) in tabs" 
        :key="index"
        @click="currentTab = index"
        :class="{ active: currentTab === index }"
      >
        {{ tab.title }}
      </button>
    </div>
    <div class="iframe-container">
      <iframe 
        v-for="(tab, index) in tabs" 
        :key="index"
        v-show="currentTab === index"
        :src="tab.url"
        frameborder="0"
      ></iframe>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentTab: 0,
      tabs: [
        { title: 'Google', url: 'https://www.google.com' },
        { title: 'Baidu', url: 'https://www.baidu.com' },
        { title: 'GitHub', url: 'https://github.com' }
      ]
    };
  }
};
</script>

<style>
.tabs button {
  padding: 8px 16px;
  margin-right: 4px;
  cursor: pointer;
}
.tabs button.active {
  background-color: #42b983;
  color: white;
}
.iframe-container {
  width: 100%;
  height: 500px;
}
iframe {
  width: 100%;
  height: 100%;
}
</style>

结合 Vue Router

若需通过路由管理 Tab,可以使用嵌套路由或动态路由参数。

<template>
  <div>
    <router-link 
      v-for="(tab, index) in tabs" 
      :key="index"
      :to="tab.path"
      custom
      v-slot="{ navigate }"
    >
      <button @click="navigate" :class="{ active: $route.path === tab.path }">
        {{ tab.title }}
      </button>
    </router-link>
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tabs: [
        { title: 'Google', path: '/google' },
        { title: 'Baidu', path: '/baidu' }
      ]
    };
  }
};
</script>

路由配置示例(需在 router.js 中定义):

const routes = [
  {
    path: '/google',
    component: () => import('./components/GoogleIframe.vue')
  },
  {
    path: '/baidu',
    component: () => import('./components/BaiduIframe.vue')
  }
];

动态加载 iframe 的组件

为每个 Tab 创建独立的组件,通过 component :is 动态加载。

<template>
  <div>
    <button 
      v-for="(tab, index) in tabs" 
      :key="index"
      @click="currentTab = tab.component"
      :class="{ active: currentTab === tab.component }"
    >
      {{ tab.title }}
    </button>
    <component :is="currentTab"></component>
  </div>
</template>

<script>
import GoogleIframe from './GoogleIframe.vue';
import BaiduIframe from './BaiduIframe.vue';

export default {
  components: { GoogleIframe, BaiduIframe },
  data() {
    return {
      currentTab: 'GoogleIframe',
      tabs: [
        { title: 'Google', component: 'GoogleIframe' },
        { title: 'Baidu', component: 'BaiduIframe' }
      ]
    };
  }
};
</script>

注意事项

  • 跨域限制:iframe 加载外部 URL 可能受浏览器同源策略限制,部分网站会禁止嵌套。
  • 性能优化:频繁切换 iframe 可能导致内存占用过高,可考虑使用 v-show 替代 v-if 减少重渲染。
  • 通信需求:若需与 iframe 内页面交互,需通过 postMessage 实现跨域通信。

vue iframe 实现tab

标签: vueiframe
分享给朋友:

相关文章

vue 页签实现

vue 页签实现

Vue 页签实现方法 在 Vue 中实现页签功能通常涉及动态组件、路由或状态管理。以下是几种常见的实现方式: 使用动态组件 通过 Vue 的 <component> 动态组件结合 v-…

vue实现简单的弹窗

vue实现简单的弹窗

使用 Vue 实现简单弹窗 组件基础结构 创建一个名为 Modal.vue 的组件文件,包含模板、脚本和样式部分: <template> <div class="modal-…

vue 实现简单登陆

vue 实现简单登陆

以下是一个使用 Vue 3 实现的简单登录功能示例,包含表单验证和基础交互逻辑: 创建 Vue 组件 <template> <div class="login-containe…

vue手写签名如何实现

vue手写签名如何实现

实现手写签名的基本思路 在Vue中实现手写签名功能,通常需要借助HTML5的Canvas API来捕获用户的绘制操作。核心流程包括初始化画布、监听触摸/鼠标事件、记录轨迹以及生成签名图片。 安装依…

vue穿梭框组件实现

vue穿梭框组件实现

实现基础穿梭框组件 在Vue中实现穿梭框组件,可以使用<el-transfer>(Element UI)或手动实现。以下是手动实现的核心逻辑: <template> &l…

vue实现导航栏切图

vue实现导航栏切图

Vue 实现导航栏切图 使用 Vue Router 实现基础导航 在 Vue 项目中,可以通过 Vue Router 实现导航栏的路由切换。创建一个导航栏组件,并在其中使用 <router-li…