当前位置:首页 > 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.js 通过npm安装Vue.js是最常见的方式。确保已安装Node.js和npm,运行以下命令: npm install vue 或者使用CDN直接引入: <script src…

vue实现模块

vue实现模块

Vue 实现模块化的方法 Vue 支持多种模块化开发方式,可以根据项目需求选择适合的方案。 使用单文件组件(SFC) 单文件组件是 Vue 最常用的模块化方式,将模板、脚本和样式封装在一个 .vue…

vue ref实现

vue ref实现

Vue 中 ref 的实现 在 Vue 中,ref 是一个用于创建响应式引用的 API,适用于基本类型和对象。以下是 ref 的核心实现原理和使用方法。 基本实现原理 ref 通过 Reactive…

vue底部实现

vue底部实现

Vue 底部实现方法 在 Vue 项目中实现底部布局可以通过多种方式完成,以下是一些常见的方法: 使用固定定位 将底部元素固定在页面底部,适用于单页应用或需要始终显示的底部栏。 <temp…

vue实现登录拦截

vue实现登录拦截

实现登录拦截的方法 在Vue项目中,可以通过路由守卫(Navigation Guards)实现登录拦截,确保未登录用户无法访问受保护的路由。以下是具体实现方式: 使用全局前置守卫 在路由配置文件中(…

vue实现转盘抽奖

vue实现转盘抽奖

Vue 实现转盘抽奖 基本思路 转盘抽奖的核心是通过 CSS 动画实现旋转效果,Vue 负责控制旋转逻辑和数据绑定。关键点包括转盘扇形区域的绘制、旋转动画的触发与停止、中奖结果的判定。 实现步骤 H…