通过 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  select实现

vue select实现

Vue Select 实现方法 在 Vue 中实现下拉选择功能可以使用原生 <select> 标签或第三方库如 vue-select。以下是两种方法的详细说明: 原生 HTML Sele…

vue element实现

vue element实现

Vue Element UI 实现 Vue Element UI 是基于 Vue.js 的组件库,提供丰富的 UI 组件,适合快速开发企业级中后台产品。以下是一些关键实现方法和步骤: 安装 Vue…

vue实现注册

vue实现注册

Vue 实现注册功能 注册功能通常涉及前端表单、数据验证、与后端 API 的交互等步骤。以下是一个完整的 Vue 实现注册功能的方案。 创建注册表单 使用 Vue 的模板语法创建注册表单,包含用户名…

vue实现openoffice

vue实现openoffice

Vue 中集成 OpenOffice 的实现方法 在 Vue 项目中集成 OpenOffice 通常需要通过后端服务或现有库实现文档的预览和编辑功能。以下是几种常见的实现方式: 使用 OnlyOff…

vue实现fragment

vue实现fragment

Vue 实现 Fragment 的方法 在 Vue 中,Fragment 允许组件返回多个根节点而不需要包裹一个额外的 DOM 元素。以下是几种实现方式: 使用 Vue 3 的 <templa…

vue怎么实现 tab

vue怎么实现 tab

Vue 实现 Tab 的方法 使用动态组件和 v-if 通过动态组件或 v-if 指令切换不同 Tab 内容,结合点击事件改变当前激活的 Tab。 <template> &l…