当前位置:首页 > VUE

vue怎么实现 tab

2026-02-10 18:26:55VUE

Vue 实现 Tab 的方法

使用 v-if 和 v-show 控制显示

通过绑定 v-ifv-show 动态切换 Tab 内容,配合点击事件改变当前激活的 Tab 索引。

<template>
  <div>
    <div class="tabs">
      <button 
        v-for="(tab, index) in tabs" 
        :key="index"
        @click="activeTab = index"
        :class="{ 'active': activeTab === index }"
      >
        {{ tab.title }}
      </button>
    </div>
    <div class="tab-content">
      <div v-show="activeTab === 0">
        Content for Tab 1
      </div>
      <div v-show="activeTab === 1">
        Content for Tab 2
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeTab: 0,
      tabs: [
        { title: 'Tab 1' },
        { title: 'Tab 2' }
      ]
    }
  }
}
</script>

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

使用动态组件 <component>

结合动态组件和 :is 属性,通过组件名动态渲染不同 Tab 内容。

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

<script>
import Tab1 from './Tab1.vue'
import Tab2 from './Tab2.vue'

export default {
  components: { Tab1, Tab2 },
  data() {
    return {
      activeTab: 'Tab1',
      tabs: [
        { title: 'Tab 1', component: 'Tab1' },
        { title: 'Tab 2', component: 'Tab2' }
      ]
    }
  }
}
</script>

使用 Vue Router 实现路由级 Tab

通过路由配置实现 Tab 切换,适合需要 URL 同步的场景。

// router.js
const routes = [
  { path: '/tab1', component: Tab1 },
  { path: '/tab2', component: Tab2 }
]
<template>
  <div>
    <router-link to="/tab1" active-class="active">Tab 1</router-link>
    <router-link to="/tab2" active-class="active">Tab 2</router-link>
    <router-view></router-view>
  </div>
</template>

使用第三方库

例如 vue-tabs-component 快速实现功能完整的 Tab。

vue怎么实现 tab

npm install vue-tabs-component
<template>
  <tabs>
    <tab name="First tab">
      Content for first tab
    </tab>
    <tab name="Second tab">
      Content for second tab
    </tab>
  </tabs>
</template>

<script>
import { Tabs, Tab } from 'vue-tabs-component'

export default {
  components: { Tabs, Tab }
}
</script>

注意事项

  • 性能考虑:v-if 会销毁组件,v-show 仅切换显示(适合频繁切换)。
  • 路由方式适合需要浏览器历史记录的场景。
  • 动态组件方式适合需要复用独立逻辑的场景。

标签: vuetab
分享给朋友:

相关文章

利用vue 实现

利用vue 实现

以下是利用 Vue 实现常见功能的几种方法,分为不同场景和技术要点: 数据绑定与响应式更新 Vue 的核心特性是数据绑定,通过 v-model 实现双向绑定: <template>…

vue实现弹幕

vue实现弹幕

Vue 实现弹幕功能 弹幕功能通常包括动态生成、滚动显示、颜色和速度随机化等特性。以下是基于 Vue 3 的实现方法。 核心思路 使用 CSS 动画控制弹幕从右向左滚动。 动态生成弹幕数据,随机设置…

vue实现 treegrid

vue实现 treegrid

Vue 实现 TreeGrid 的方法 使用现成组件库 Element UI、Ant Design Vue 等流行组件库提供了现成的 TreeTable/TreeGrid 组件,可直接使用。 Ele…

vue实现tip

vue实现tip

Vue实现Tooltip的方法 使用Vue实现Tooltip可以通过多种方式完成,以下是几种常见的方法: 使用原生HTML的title属性 在Vue模板中直接使用HTML的title属性是最简单的实…

vue 实现原理

vue 实现原理

Vue 实现原理 Vue.js 是一个渐进式 JavaScript 框架,其核心实现原理主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是 Vue 实现原理的核心要点: 响应式系统 Vu…

vue实现数据联动

vue实现数据联动

Vue 数据联动实现方法 数据联动在 Vue 中通常指多个组件或数据项之间的动态关联更新。以下是几种常见实现方式: 使用 v-model 和计算属性 <template> <d…