当前位置:首页 > 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 动态加载。

vue iframe 实现tab

<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 实现跨域通信。

标签: vueiframe
分享给朋友:

相关文章

vue实现浮标

vue实现浮标

Vue 实现浮动按钮(浮标) 使用 Vue 实现浮动按钮可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 定位和 Vue 组件 创建 Vue 组件并配合 CSS 固定定位实现浮动按钮:…

vue 简单实现

vue 简单实现

以下是 Vue 的简单实现示例,涵盖基础模板、数据绑定、方法及生命周期: 基础模板与数据绑定 <div id="app"> <p>{{ message }}</p&…

vue实现快手

vue实现快手

Vue 实现类似快手的功能 Vue.js 是一个渐进式 JavaScript 框架,适合构建复杂的单页应用。要实现类似快手的功能,可以结合 Vue 和相关技术栈进行开发。 核心功能模块 视频流展示…

vue 实现多选框

vue 实现多选框

Vue 实现多选框的方法 使用 v-model 绑定数组 在 Vue 中,可以通过 v-model 绑定一个数组来实现多选框的选择功能。每个复选框的 value 属性会被添加到数组中,取消勾选时则从数…

vue视频怎么实现

vue视频怎么实现

实现视频播放功能 在Vue中实现视频播放功能可以通过HTML5的<video>标签或第三方库来实现。以下是几种常见的方法: 使用HTML5 <video>标签 <te…

vue怎么实现滑动

vue怎么实现滑动

Vue实现滑动效果的方法 在Vue中实现滑动效果可以通过多种方式,以下是几种常见的方法: 使用CSS过渡和动画 通过Vue的过渡系统和CSS可以实现平滑的滑动效果。定义一个CSS类,利用transf…