当前位置:首页 > VUE

用vue实现tab切换

2026-02-20 21:50:21VUE

使用 Vue 实现 Tab 切换

基础实现(选项式 API)

在 Vue 中可以通过 v-forv-showv-if 结合数据绑定实现 Tab 切换功能。

模板部分

<div class="tabs">
  <div class="tab-header">
    <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-for="(tab, index) in tabs" :key="index" v-show="activeTab === index">
      {{ tab.content }}
    </div>
  </div>
</div>

脚本部分

export default {
  data() {
    return {
      activeTab: 0,
      tabs: [
        { title: 'Tab 1', content: 'Content for Tab 1' },
        { title: 'Tab 2', content: 'Content for Tab 2' },
        { title: 'Tab 3', content: 'Content for Tab 3' }
      ]
    }
  }
}

样式部分

.tab-header button {
  padding: 8px 16px;
  cursor: pointer;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
}

.tab-header button.active {
  border-bottom-color: #42b983;
  color: #42b983;
}

.tab-content {
  margin-top: 16px;
}

组合式 API 实现

使用 Vue 3 的组合式 API 可以更简洁地实现相同功能。

<script setup>
import { ref } from 'vue'

const activeTab = ref(0)
const tabs = ref([
  { title: 'Tab 1', content: 'Content for Tab 1' },
  { title: 'Tab 2', content: 'Content for Tab 2' },
  { title: 'Tab 3', content: 'Content for Tab 3' }
])
</script>

<template>
  <!-- 模板部分与选项式API相同 -->
</template>

动态内容加载

如果需要动态加载 Tab 内容,可以结合异步组件或动态导入。

const tabs = ref([
  { 
    title: 'User Profile',
    component: defineAsyncComponent(() => import('./UserProfile.vue'))
  },
  {
    title: 'Settings',
    component: defineAsyncComponent(() => import('./Settings.vue'))
  }
])
<component :is="tabs[activeTab].component" />

路由集成

将 Tab 切换与 Vue Router 集成可以实现基于 URL 的 Tab 状态管理。

const routes = [
  { path: '/', component: Home, children: [
    { path: 'tab1', component: Tab1 },
    { path: 'tab2', component: Tab2 }
  ]}
]
<router-link to="/tab1">Tab 1</router-link>
<router-link to="/tab2">Tab 2</router-link>
<router-view></router-view>

可复用组件

创建一个可复用的 Tab 组件提高代码复用性。

<!-- TabContainer.vue -->
<template>
  <div class="tab-container">
    <slot name="header" :tabs="tabs" :activeTab="activeTab" :setActiveTab="setActiveTab"></slot>
    <slot name="content" :activeTabContent="tabs[activeTab].content"></slot>
  </div>
</template>

<script setup>
const props = defineProps({
  tabs: { type: Array, required: true },
  initialTab: { type: Number, default: 0 }
})

const activeTab = ref(props.initialTab)
const setActiveTab = (index) => { activeTab.value = index }
</script>

使用时:

<TabContainer :tabs="tabs">
  <template #header="{ tabs, activeTab, setActiveTab }">
    <!-- 自定义头部 -->
  </template>
  <template #content="{ activeTabContent }">
    <!-- 自定义内容 -->
  </template>
</TabContainer>

用vue实现tab切换

标签: vuetab
分享给朋友:

相关文章

vue实现后退

vue实现后退

Vue 实现后退功能的方法 在 Vue 中实现后退功能通常可以通过以下几种方式完成,具体取决于应用场景和需求。 使用 window.history API 通过原生 JavaScript 的 wi…

vue实现影院

vue实现影院

Vue实现影院功能的基本思路 使用Vue框架开发影院功能需要结合前端组件化开发、状态管理以及后端API交互。以下是关键实现步骤: 影院页面布局设计 影院页面通常分为顶部导航栏、影院列表区域和底部导…

vue拼图实现

vue拼图实现

实现 Vue 拼图游戏的方法 使用 Vue 组件和动态数据绑定 创建一个 Vue 组件来管理拼图的状态和逻辑。通过 v-for 动态渲染拼图块,利用 v-bind 绑定样式和位置。拼图块的数据可以存储…

vue实现尺子

vue实现尺子

Vue 实现尺子组件 实现一个尺子组件可以通过 Canvas 或纯 CSS 完成。以下是两种常见实现方式: 使用 Canvas 绘制尺子 通过 Canvas 动态绘制刻度线和数字,适合需要高精度或动…

vue远离实现

vue远离实现

Vue 原理实现 Vue.js 的核心原理包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是其关键实现原理的详细解析。 响应式系统 Vue 的响应式系统基于 Object.definePr…

vue实现城市

vue实现城市

Vue 实现城市选择功能 使用 Element UI 的 Cascader 组件 Element UI 提供了一个 Cascader 级联选择器组件,非常适合实现城市选择功能。需要先安装 Elemen…