通过 Vue 的 component 动态组件和 v-for 指令可以轻松实现 Tab 页功能。核心思路…">
当前位置:首页 > VUE

vue 实现tab页

2026-02-18 04:47:43VUE

Vue 实现 Tab 页的方法

使用动态组件 <component :is="">

通过 Vue 的 component 动态组件和 v-for 指令可以轻松实现 Tab 页功能。核心思路是利用 v-for 渲染 Tab 标题,并通过 v-modelv-bind 控制当前激活的 Tab。

vue 实现tab页

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

<script>
export default {
  data() {
    return {
      tabs: ['Home', 'About', 'Contact'],
      currentTab: 'Home'
    };
  },
  computed: {
    currentTabComponent() {
      return this.currentTab.toLowerCase() + '-tab';
    }
  }
};
</script>

<style>
.active {
  background-color: #42b983;
  color: white;
}
</style>

使用 v-ifv-show 控制显示

另一种方法是直接通过 v-ifv-show 控制 Tab 内容的显示与隐藏。v-show 通过 CSS 的 display 属性切换,适合频繁切换的场景;v-if 会销毁和重建 DOM,适合不频繁切换的场景。

vue 实现tab页

<template>
  <div>
    <div class="tabs">
      <button 
        v-for="(tab, index) in tabs" 
        :key="index"
        @click="currentTab = tab.name"
        :class="{ active: currentTab === tab.name }"
      >
        {{ tab.label }}
      </button>
    </div>
    <div class="tab-content">
      <div v-show="currentTab === 'home'">Home Content</div>
      <div v-show="currentTab === 'about'">About Content</div>
      <div v-show="currentTab === 'contact'">Contact Content</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tabs: [
        { name: 'home', label: 'Home' },
        { name: 'about', label: 'About' },
        { name: 'contact', label: 'Contact' }
      ],
      currentTab: 'home'
    };
  }
};
</script>

使用 Vue Router 实现路由级 Tab

如果需要 Tab 页与 URL 同步,可以通过 Vue Router 实现路由级 Tab。每个 Tab 对应一个路由路径,适合复杂应用。

// router.js
const routes = [
  { path: '/', component: Home, name: 'home' },
  { path: '/about', component: About, name: 'about' },
  { path: '/contact', component: Contact, name: 'contact' }
];
<template>
  <div>
    <router-link 
      v-for="(tab, index) in tabs" 
      :key="index"
      :to="{ name: tab.name }"
      active-class="active"
    >
      {{ tab.label }}
    </router-link>
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tabs: [
        { name: 'home', label: 'Home' },
        { name: 'about', label: 'About' },
        { name: 'contact', label: 'Contact' }
      ]
    };
  }
};
</script>

封装可复用的 Tab 组件

为了提高复用性,可以封装一个通用的 Tab 组件,通过插槽(Slots)传递内容。

<!-- Tabs.vue -->
<template>
  <div>
    <div class="tabs-header">
      <button 
        v-for="(tab, index) in tabs" 
        :key="index"
        @click="currentTab = tab.name"
        :class="{ active: currentTab === tab.name }"
      >
        {{ tab.label }}
      </button>
    </div>
    <div class="tabs-content">
      <slot :name="currentTab"></slot>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    tabs: Array,
    initialTab: String
  },
  data() {
    return {
      currentTab: this.initialTab || this.tabs[0].name
    };
  }
};
</script>
<!-- Parent.vue -->
<template>
  <Tabs :tabs="tabs" initialTab="home">
    <template #home>Home Content</template>
    <template #about>About Content</template>
    <template #contact>Contact Content</template>
  </Tabs>
</template>

<script>
import Tabs from './Tabs.vue';

export default {
  components: { Tabs },
  data() {
    return {
      tabs: [
        { name: 'home', label: 'Home' },
        { name: 'about', label: 'About' },
        { name: 'contact', label: 'Contact' }
      ]
    };
  }
};
</script>

注意事项

  • 动态组件需提前注册或异步加载。
  • 路由级 Tab 需确保路由配置正确。
  • 封装组件时注意插槽命名和作用域。

标签: vuetab
分享给朋友:

相关文章

vue实现选择分类

vue实现选择分类

Vue 实现选择分类的方法 使用 v-model 绑定选择值 在 Vue 中可以通过 v-model 指令实现双向数据绑定,适用于下拉选择框(<select>)。以下是一个基本示例: &…

vue实现视频开场

vue实现视频开场

使用 Vue 实现视频开场动画 在 Vue 中实现视频开场动画可以通过结合 HTML5 的 <video> 标签和 Vue 的过渡效果或生命周期钩子来完成。以下是具体实现方法: 安装依赖…

vue弹幕实现不重叠

vue弹幕实现不重叠

实现 Vue 弹幕不重叠的方法 CSS 定位与动画控制 通过绝对定位和动态计算弹幕轨道高度,确保弹幕在不同轨道上运行。每条弹幕占据固定高度的轨道,避免重叠。 // 弹幕轨道高度计算 const t…

vue实现简单的弹窗

vue实现简单的弹窗

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

vue 实现toast

vue 实现toast

vue 实现 toast 的方法 在 Vue 中实现 Toast 提示功能可以通过多种方式,以下是几种常见的实现方法: 使用第三方库 Vue 生态中有许多成熟的 Toast 库,例如 vue-to…

ssh vue 实现

ssh vue 实现

使用SSH连接Vue项目部署的服务器 在Vue项目开发或部署过程中,可能需要通过SSH连接到远程服务器进行配置或维护。以下是实现SSH连接并部署Vue项目的关键步骤: 生成SSH密钥对 在本地终端…