通过 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。

<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,适合不频繁切换的场景。

<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)传递内容。

vue 实现tab页

<!-- 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 实现边框的方法 在 Vue 中实现边框效果可以通过多种方式完成,包括内联样式、CSS 类绑定、动态样式以及使用第三方 UI 库。以下是几种常见的实现方法。 内联样式绑定 使用 Vue 的 :…

vue实现modal

vue实现modal

Vue 实现 Modal 弹窗组件 使用 Vue 实现 Modal 弹窗组件可以通过多种方式,包括原生 Vue 组件、第三方库或自定义指令。以下是几种常见方法: 原生 Vue 组件实现 创建一个基础…

vue实现slot

vue实现slot

Vue 中的 Slot 实现方法 Vue 的 slot 是一种内容分发机制,允许父组件向子组件插入内容。以下是几种常见的 Slot 实现方式: 默认 Slot 子组件通过 <slot>…

vue实现slidetoggle

vue实现slidetoggle

Vue 实现 SlideToggle 效果 SlideToggle 是一种常见的交互效果,元素以滑动方式展开或收起。以下是几种实现方法: 使用 CSS Transition 和 v-show…

vue 实现$(id)

vue 实现$(id)

在 Vue 中实现类似 jQuery 的 $(id) 功能,可以通过以下几种方式操作 DOM 元素: 使用 ref 属性 Vue 推荐使用 ref 属性来直接访问 DOM 元素或组件实例。在模板中为…

vue实现eventbus

vue实现eventbus

Vue 中实现 EventBus 在 Vue 中,EventBus 是一种跨组件通信的机制,尤其适用于非父子组件之间的数据传递。以下是实现 EventBus 的几种方法: 方法一:使用 Vue 实例…