当前位置:首页 > VUE

vue 实现tabs

2026-03-06 23:27:26VUE

Vue 实现 Tabs 组件的方法

基础实现

创建一个基础的 Tabs 组件需要定义两个子组件:TabsTabTabs 作为容器,Tab 作为内容项。

Tabs.vue

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

<script>
import { ref, provide, computed } from 'vue'

export default {
  setup() {
    const tabs = ref([])
    const activeIndex = ref(0)

    const selectTab = (index) => {
      activeIndex.value = index
    }

    provide('tabsContext', {
      tabs,
      activeIndex
    })

    return {
      tabs,
      activeIndex,
      selectTab
    }
  }
}
</script>

Tab.vue

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

<script>
import { inject, computed, onMounted } from 'vue'

export default {
  props: {
    title: {
      type: String,
      required: true
    }
  },

  setup(props) {
    const { tabs, activeIndex } = inject('tabsContext')
    const isActive = computed(() => {
      return tabs.value[activeIndex.value] === instance
    })

    const instance = { props }

    onMounted(() => {
      tabs.value.push(instance)
    })

    return {
      isActive
    }
  }
}
</script>

使用示例

<template>
  <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>
</template>

<script>
import Tabs from './Tabs.vue'
import Tab from './Tab.vue'

export default {
  components: {
    Tabs,
    Tab
  }
}
</script>

进阶功能

动态添加/删除 Tab 可以通过修改 tabs 数组来实现动态添加或删除 Tab。在 Tabs 组件中暴露添加和删除的方法:

// 在 Tabs.vue 的 setup 中添加
const addTab = (tab) => {
  tabs.value.push(tab)
}

const removeTab = (index) => {
  tabs.value.splice(index, 1)
  if (activeIndex.value >= tabs.value.length) {
    activeIndex.value = tabs.value.length - 1
  }
}

provide('tabsContext', {
  tabs,
  activeIndex,
  addTab,
  removeTab
})

动画效果 可以为 Tab 切换添加过渡动画:

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

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

路由集成 可以与 Vue Router 集成,使每个 Tab 对应一个路由:

// 在 Tabs.vue 中
import { useRoute, useRouter } from 'vue-router'

const route = useRoute()
const router = useRouter()

// 根据路由路径设置 activeIndex
const updateActiveIndex = () => {
  const index = tabs.value.findIndex(tab => tab.props.to === route.path)
  if (index !== -1) {
    activeIndex.value = index
  }
}

// Tab 点击时导航到对应路由
const selectTab = (index) => {
  const tab = tabs.value[index]
  if (tab.props.to) {
    router.push(tab.props.to)
  }
  activeIndex.value = index
}

样式定制

可以通过 CSS 变量或 props 来自定义样式:

vue 实现tabs

<template>
  <div class="tabs" :style="{
    '--active-color': activeColor,
    '--inactive-color': inactiveColor
  }">
    <!-- ... -->
  </div>
</template>

<script>
export default {
  props: {
    activeColor: {
      type: String,
      default: '#42b983'
    },
    inactiveColor: {
      type: String,
      default: '#2c3e50'
    }
  }
}
</script>

<style>
.tabs-header button.active {
  color: var(--active-color);
  border-bottom: 2px solid var(--active-color);
}

.tabs-header button {
  color: var(--inactive-color);
}
</style>

标签: vuetabs
分享给朋友:

相关文章

vue设计与实现目录

vue设计与实现目录

以下是《Vue设计与实现》一书的典型目录结构(根据常见技术书籍及Vue核心内容整理): 响应式系统 响应式数据的基本原理 依赖收集与触发更新 实现简易响应式系统 Proxy与Reflec…

vue公共列表的实现

vue公共列表的实现

实现 Vue 公共列表组件 公共列表组件通常用于展示重复结构的数据,例如商品列表、用户列表等。以下是一个典型的实现方式: 基础实现 创建可复用的列表组件 List.vue: <tem…

vue实现autocomplete

vue实现autocomplete

Vue 实现 Autocomplete 功能 Vue 中实现 Autocomplete(自动完成)功能可以通过自定义组件或使用现有库完成。以下是几种常见方法: 方法一:使用 Vue 原生实现 创建…

vue实现跑车

vue实现跑车

Vue 实现跑车动画效果 使用 Vue 结合 CSS 动画可以实现跑车在屏幕上移动的效果。以下是一个完整的实现方案: 创建 Vue 组件结构 <template> <div c…

jq 实现 vue

jq 实现 vue

jq 实现 Vue 的核心功能 jq 是一个轻量级的 JavaScript 库,主要用于 DOM 操作和事件处理。虽然它不能完全替代 Vue,但可以通过一些技巧模拟 Vue 的核心功能,如数据绑定、事…

vue实现盖章

vue实现盖章

Vue 实现盖章功能 在Vue中实现盖章功能通常涉及前端UI交互,通过Canvas或SVG绘制印章,并允许用户拖拽或点击放置印章。以下是具体实现方法: 使用Canvas绘制印章 通过HTML5的Ca…