当前位置:首页 > 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 alert实现

vue alert实现

使用 Vue 实现 Alert 组件 在 Vue 中实现 Alert 组件可以通过自定义组件或结合第三方库完成。以下是几种常见方法: 自定义 Alert 组件 创建一个可复用的 Alert 组件,通…

vue 全景实现

vue 全景实现

Vue 全景实现方案 在Vue中实现全景效果,通常需要结合WebGL或第三方库来处理3D场景渲染。以下是几种常见方法: 使用Three.js库 Three.js是一个强大的WebGL库,适合创建3D…

实现vue table

实现vue table

Vue 表格实现方法 基础表格实现 使用 Vue 的 v-for 指令可以快速渲染表格数据。在模板中定义表格结构,通过数据绑定动态生成行和列。 <template> <tabl…

vue 动画实现

vue 动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要分为内置组件和第三方库集成。 使用 Vue 内置过渡组件 Vue 的 <transition> 和 <transiti…

vue工序实现

vue工序实现

Vue 工序实现方法 在Vue中实现工序(或流程)功能,通常涉及状态管理、组件通信和动态渲染。以下是几种常见方法: 使用动态组件 通过Vue的<component :is="currentCo…

实现简单vue

实现简单vue

实现一个简单的 Vue.js 框架 Vue.js 的核心功能包括数据绑定、响应式系统和虚拟 DOM。以下是一个简化版的 Vue 实现,涵盖基本功能。 数据响应式系统 使用 Object.define…