当前位置:首页 > VUE

vue组件实现tab

2026-02-19 10:05:31VUE

Vue 组件实现 Tab 切换功能

基础实现方案

创建两个组件:Tabs.vue(父组件)和 Tab.vue(子组件)。父组件管理当前激活的 tab,子组件作为单个 tab 的内容容器。

Tabs.vue

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

<script>
export default {
  data() {
    return {
      tabs: [],
      currentTab: 0
    }
  },
  mounted() {
    this.tabs = this.$children.map(child => ({
      title: child.title
    }))
  }
}
</script>

<style>
.tabs-header button.active {
  background: #ddd;
}
</style>

Tab.vue

<template>
  <div v-show="isActive">
    <slot></slot>
  </div>
</template>

<script>
export default {
  props: {
    title: {
      type: String,
      required: true
    }
  },
  computed: {
    isActive() {
      return this.$parent.currentTab === this.$parent.tabs.findIndex(tab => tab.title === this.title)
    }
  }
}
</script>

使用示例

<tabs>
  <tab title="Tab 1">
    Content for tab 1
  </tab>
  <tab title="Tab 2">
    Content for tab 2
  </tab>
  <tab title="Tab 3">
    Content for tab 3
  </tab>
</tabs>

进阶优化方案

使用 provide/inject 代替直接访问 $parent,使组件更解耦:

Tabs.vue

<script>
export default {
  provide() {
    return {
      tabsProvider: this
    }
  },
  // 其余代码不变
}
</script>

Tab.vue

<script>
export default {
  inject: ['tabsProvider'],
  computed: {
    isActive() {
      return this.tabsProvider.currentTab === this.tabsProvider.tabs.findIndex(tab => tab.title === this.title)
    }
  }
}
</script>

动画效果实现

为 tab 内容切换添加过渡动画:

<template>
  <transition name="fade" mode="out-in">
    <div v-show="isActive" :key="title">
      <slot></slot>
    </div>
  </transition>
</template>

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

响应式设计

添加响应式样式,使 tabs 在移动设备上表现良好:

vue组件实现tab

.tabs-header {
  display: flex;
  overflow-x: auto;
}

.tabs-header button {
  white-space: nowrap;
  padding: 10px 20px;
  border: none;
  background: none;
  cursor: pointer;
}

@media (max-width: 768px) {
  .tabs-header {
    flex-wrap: wrap;
  }
  .tabs-header button {
    flex: 1 0 auto;
  }
}

标签: 组件vue
分享给朋友:

相关文章

vue 实现fadeout

vue 实现fadeout

Vue 实现淡出效果 在 Vue 中实现淡出(fade-out)效果可以通过 CSS 过渡或动画结合 Vue 的动态渲染功能完成。以下是几种常见方法: 使用 Vue 过渡(Transition)组件…

vue 实现搜索

vue 实现搜索

实现 Vue 搜索功能 在 Vue 中实现搜索功能通常涉及以下几个关键步骤: 数据绑定与输入监听 使用 v-model 双向绑定搜索输入框的值,监听用户输入: <template>…

vue实现treeview

vue实现treeview

Vue 实现 TreeView 的方法 使用递归组件实现 TreeView 递归组件是 Vue 中实现 TreeView 的常见方式。通过组件调用自身,可以轻松处理嵌套的树形结构数据。 <te…

vue实现curd

vue实现curd

Vue实现CRUD操作 初始化Vue项目 使用Vue CLI创建新项目,安装必要依赖如axios用于HTTP请求 vue create crud-demo cd crud-demo npm inst…

vue最佳实现

vue最佳实现

Vue 最佳实践 组件设计 组件应遵循单一职责原则,每个组件只负责一个功能模块。命名采用 PascalCase(如 UserProfile.vue),模板中使用 kebab-case(如 <us…

vue实现爬虫

vue实现爬虫

Vue 实现爬虫的基本思路 Vue.js 本身是一个前端框架,主要用于构建用户界面。要实现爬虫功能,通常需要结合后端技术或浏览器自动化工具。以下是几种常见的方法: 方法一:Vue + Node.js…