当前位置:首页 > VUE

vue实现切换tab

2026-03-29 17:49:37VUE

Vue 实现 Tab 切换功能

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

通过 v-ifv-show 指令结合点击事件实现 Tab 内容的切换。v-if 会销毁和重建 DOM 元素,适合切换频率低的场景;v-show 仅切换 CSS 的 display 属性,适合频繁切换的场景。

vue实现切换tab

<template>
  <div>
    <div class="tabs">
      <button 
        v-for="(tab, index) in tabs" 
        :key="index"
        @click="currentTab = index"
        :class="{ active: currentTab === index }"
      >
        {{ tab.name }}
      </button>
    </div>
    <div class="content">
      <div v-if="currentTab === 0">内容 1</div>
      <div v-if="currentTab === 1">内容 2</div>
      <div v-if="currentTab === 2">内容 3</div>
    </div>
  </div>
</template>

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

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

动态组件实现

通过 Vue 的 <component> 动态组件和 :is 属性实现更灵活的 Tab 切换,适合每个 Tab 内容为独立组件的情况。

vue实现切换tab

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

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

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

使用 Vue Router 实现

如果 Tab 切换需要 URL 同步或更复杂的状态管理,可以使用 Vue Router 的嵌套路由。

// router.js
const routes = [
  {
    path: '/',
    component: TabsContainer,
    children: [
      { path: '', component: Tab1 },
      { path: 'tab1', component: Tab1 },
      { path: 'tab2', component: Tab2 },
      { path: 'tab3', component: Tab3 }
    ]
  }
]
<!-- TabsContainer.vue -->
<template>
  <div>
    <router-link to="/tab1">Tab 1</router-link>
    <router-link to="/tab2">Tab 2</router-link>
    <router-link to="/tab3">Tab 3</router-link>
    <router-view />
  </div>
</template>

第三方库实现

对于更复杂的 Tab 功能(如动画、懒加载),可以使用第三方库如 vue-tabs-component

npm install vue-tabs-component
<template>
  <tabs>
    <tab name="First tab">
      First tab content
    </tab>
    <tab name="Second tab">
      Second tab content
    </tab>
  </tabs>
</template>

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

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

以上方法根据需求选择,简单场景用 v-if 或动态组件即可,复杂场景推荐 Vue Router 或第三方库。

标签: vuetab
分享给朋友:

相关文章

vue调用接口实现退出

vue调用接口实现退出

实现Vue退出功能的步骤 在Vue中调用接口实现退出功能通常需要与后端API交互,以下是实现方法: 前端实现 创建退出方法,调用后端API接口: methods: { logout()…

vue element实现

vue element实现

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

实现 vue 框架

实现 vue 框架

Vue 框架的基本实现 Vue 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现 Vue 框架的基本步骤和核心概念。 核心概念 响应式数据绑定 Vue 的核心特性之一是响应式数…

vue实现switch

vue实现switch

Vue 实现 Switch 开关组件 在 Vue 中实现 Switch 开关组件可以通过自定义组件或使用现有 UI 库完成。以下是几种常见实现方式: 自定义 Switch 组件 创建一个基础 Sw…

vue 实现过滤

vue 实现过滤

Vue 实现过滤的方法 在 Vue 中实现过滤功能可以通过多种方式完成,包括使用计算属性、自定义过滤器、第三方库等。以下是几种常见的实现方法: 使用计算属性进行过滤 计算属性是 Vue 中最常用的过…

vue 实现脚本

vue 实现脚本

Vue 实现脚本的方法 Vue.js 提供了多种方式来实现脚本功能,包括组件内脚本、混入(Mixins)、插件(Plugins)以及自定义指令等。以下是常见的实现方式: 组件内脚本 在 Vue 单文…