当前位置:首页 > VUE

vue 实现点击tab切换

2026-02-23 16:12:32VUE

Vue 实现 Tab 切换功能

基础实现(v-show/v-if + 事件)

通过 v-showv-if 控制内容显示,配合点击事件切换状态:

vue 实现点击tab切换

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

    <div class="content">
      <div v-show="currentTab === 0">内容1</div>
      <div v-show="currentTab === 1">内容2</div>
      <!-- 或使用动态渲染 -->
      <div>{{ tabs[currentTab].content }}</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentTab: 0,
      tabs: [
        { title: 'Tab1', content: '内容1' },
        { title: 'Tab2', content: '内容2' }
      ]
    }
  }
}
</script>

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

动态组件实现(component + is)

通过动态组件实现更灵活的切换:

vue 实现点击tab切换

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

    <component :is="currentTabComponent"></component>
  </div>
</template>

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

export default {
  components: { Tab1, Tab2 },
  data() {
    return {
      currentTab: 'Tab1',
      tabs: {
        Tab1: { title: '标签1' },
        Tab2: { title: '标签2' }
      }
    }
  },
  computed: {
    currentTabComponent() {
      return this.currentTab
    }
  }
}
</script>

路由集成方案

结合 vue-router 实现带 URL 状态的 Tab:

// router.js
const routes = [
  { path: '/tab1', component: Tab1 },
  { path: '/tab2', component: Tab2 }
]
<template>
  <router-link 
    v-for="tab in tabs"
    :key="tab.path"
    :to="tab.path"
    active-class="active"
  >
    {{ tab.title }}
  </router-link>

  <router-view></router-view>
</template>

动画过渡效果

为切换添加过渡动画:

<transition name="fade" mode="out-in">
  <component :is="currentTabComponent"></component>
</transition>

<style>
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.3s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}
</style>

注意事项

  • 频繁切换建议使用 v-show 避免组件重渲染
  • 需要保持状态时使用 keep-alive 包裹动态组件
  • 移动端可结合手势库实现滑动切换
  • 复杂场景可使用专门的状态管理(如 Pinia)维护 Tab 状态

以上方案可根据实际需求组合使用,基础项目通常采用第一种方案即可满足需求,大型应用建议采用路由集成方案。

标签: vuetab
分享给朋友:

相关文章

vue实现点击

vue实现点击

实现点击事件的基本方法 在Vue中实现点击事件可以通过v-on指令或@简写方式绑定。以下是常见的点击事件绑定方法: <template> <button v-on:click=…

vue 实现注册

vue 实现注册

Vue 实现注册功能 注册功能通常包括表单验证、数据提交和用户反馈。以下是实现注册功能的步骤: 1. 创建注册表单组件 在 Vue 项目中创建一个注册表单组件,包含用户名、邮箱、密码等输入字段。…

实现vue datepicker

实现vue datepicker

安装依赖 使用 vue-datepicker 或第三方库如 v-calendar 或 vue2-datepicker。以 vue2-datepicker 为例: npm install vue2…

vue实现心电图

vue实现心电图

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

vue环境实现

vue环境实现

Vue 环境搭建 安装 Node.js 和 npm Node.js 是运行 Vue 的基础环境,npm 是包管理工具。从 Node.js 官网 下载并安装最新稳定版本。安装完成后,在终端运行以下命令验…

js 实现vue

js 实现vue

实现 Vue 的核心功能 在 JavaScript 中实现 Vue 的核心功能需要模拟数据绑定、虚拟 DOM 和响应式系统。以下是一个简化版的实现思路: 响应式系统 通过 Object.define…