当前位置:首页 > VUE

vue怎么实现 tab

2026-02-10 18:26:55VUE

Vue 实现 Tab 的方法

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

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

vue怎么实现 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 内容。

vue怎么实现 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。

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中实现多用户登录通常需要结合后端API完成身份验证,并通过前端路由、状态管理(如Vuex或Pinia)和本地存储(如localStorage)来管理用户会话。以下是关…

vue实现ajax

vue实现ajax

Vue 中实现 AJAX 的几种方法 在 Vue 中实现 AJAX 请求可以通过原生 JavaScript 的 XMLHttpRequest、第三方库如 axios 或 fetch API 来完成。以…

vue实现心电图

vue实现心电图

Vue 实现心电图效果 在 Vue 中实现心电图效果通常需要结合 Canvas 或 SVG 进行动态绘图,模拟心电图数据的实时变化。以下是两种实现方式的核心思路和代码示例: 使用 Canvas 绘制…

vue 实现$(id)

vue 实现$(id)

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

vue实现eventbus

vue实现eventbus

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

vue实现分站

vue实现分站

Vue 实现分站的方案 在 Vue 中实现分站功能通常涉及路由配置、环境变量管理和动态加载资源。以下是几种常见的实现方式: 基于路由的分站实现 通过 Vue Router 配置多路由,区分不同站点的…