当前位置:首页 > VUE

vue实现tab标签

2026-01-11 22:05:38VUE

使用 Vue 实现 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: '标签1', content: '内容1' },
        { title: '标签2', content: '内容2' },
        { title: '标签3', content: '内容3' }
      ]
    }
  }
}

样式部分

.tab-header button {
  padding: 8px 16px;
  background: #f0f0f0;
  border: none;
  cursor: pointer;
}

.tab-header button.active {
  background: #ddd;
  font-weight: bold;
}

.tab-content div {
  padding: 16px;
  border: 1px solid #ddd;
}

组件化方案

TabContainer.vue

vue实现tab标签

<template>
  <div class="tab-container">
    <slot></slot>
  </div>
</template>

<script>
export default {
  provide() {
    return {
      tabProvider: this
    }
  },
  data() {
    return {
      activeTab: null
    }
  },
  methods: {
    setActiveTab(tab) {
      this.activeTab = tab
    }
  }
}
</script>

Tab.vue

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

<script>
export default {
  props: {
    title: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      isActive: false
    }
  },
  inject: ['tabProvider'],
  mounted() {
    this.tabProvider.setActiveTab(this)
    this.isActive = this.tabProvider.activeTab === this
  }
}
</script>

使用方式

<tab-container>
  <tab title="标签1">
    内容1
  </tab>
  <tab title="标签2">
    内容2
  </tab>
</tab-container>

动态添加标签

实现方法

vue实现tab标签

methods: {
  addTab() {
    this.tabs.push({
      title: `新标签${this.tabs.length + 1}`,
      content: `新内容${this.tabs.length + 1}`
    })
  },
  removeTab(index) {
    this.tabs.splice(index, 1)
    if (this.activeTab >= index && this.activeTab > 0) {
      this.activeTab--
    }
  }
}

路由集成方案

结合 vue-router

const routes = [
  { path: '/tab1', component: Tab1 },
  { path: '/tab2', component: Tab2 }
]

const router = new VueRouter({
  routes
})

new Vue({
  router,
  template: `
    <div>
      <router-link to="/tab1" tag="button">标签1</router-link>
      <router-link to="/tab2" tag="button">标签2</router-link>
      <router-view></router-view>
    </div>
  `
}).$mount('#app')

动画效果

添加过渡动画

<transition name="fade" mode="out-in">
  <div v-for="(tab, index) in tabs" 
       :key="index" 
       v-show="activeTab === index">
    {{ tab.content }}
  </div>
</transition>

CSS 动画

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

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

相关文章

vue架构实现

vue架构实现

Vue 架构实现核心方案 Vue.js 的架构实现通常围绕模块化、组件化和状态管理展开。以下为常见实现方案: 基于单文件组件(SFC)的模块化 通过 .vue 文件组织模板、脚本和样式: <…

vue实现bootstrap

vue实现bootstrap

Vue 中集成 Bootstrap 的方法 在 Vue 项目中集成 Bootstrap 可以通过以下方式实现,涵盖样式、组件和交互功能。 安装 Bootstrap 依赖 通过 npm 或 yarn…

vue插件实现

vue插件实现

Vue 插件实现方法 Vue 插件是一种向 Vue 应用程序添加全局功能的机制。插件可以包含全局指令、过滤器、混入、实例方法等。 插件的基本结构 一个 Vue 插件通常是一个对象或函数,需要暴露一个…

vue实现active

vue实现active

Vue 实现 active 状态的方法 在 Vue 中实现 active 状态可以通过以下几种方式实现,具体取决于使用场景和需求。 使用 v-bind:class 动态绑定类名 通过 v-bind:…

vue实现flvvideo

vue实现flvvideo

vue-flv-player 实现 FLV 视频播放 安装依赖包 npm install flv.js vue-flv-player 基础组件引入 <template> <…

vue实现开关

vue实现开关

Vue 实现开关组件的方法 使用原生HTML和Vue数据绑定 通过v-model绑定复选框的checked状态,结合CSS样式实现开关效果: <template> <labe…