动态切换组件,结合 v-for 渲染标签栏。…">
当前位置:首页 > VUE

vue组件 实现tab切换

2026-02-21 18:16:18VUE

Vue 组件实现 Tab 切换

使用动态组件实现 Tab 切换

通过 Vue 的 <component :is="currentTab"> 动态切换组件,结合 v-for 渲染标签栏。

<template>
  <div>
    <div class="tab-buttons">
      <button 
        v-for="tab in tabs" 
        :key="tab" 
        @click="currentTab = tab"
        :class="{ active: currentTab === tab }"
      >
        {{ tab }}
      </button>
    </div>
    <component :is="currentTab" class="tab-content"></component>
  </div>
</template>

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

export default {
  data() {
    return {
      tabs: ['Tab1', 'Tab2'],
      currentTab: 'Tab1'
    };
  },
  components: {
    Tab1,
    Tab2
  }
};
</script>

<style>
.tab-buttons button {
  padding: 8px 16px;
  margin-right: 4px;
}
.tab-buttons button.active {
  background: #42b983;
  color: white;
}
</style>

使用 CSS 和 v-show 实现 Tab 切换

通过 v-show 控制内容显示,适合简单场景。

vue组件 实现tab切换

<template>
  <div>
    <div class="tab-buttons">
      <button 
        v-for="tab in tabs" 
        :key="tab.name"
        @click="currentTab = tab.name"
        :class="{ active: currentTab === tab.name }"
      >
        {{ tab.label }}
      </button>
    </div>
    <div class="tab-contents">
      <div v-for="tab in tabs" :key="tab.name" v-show="currentTab === tab.name">
        {{ tab.content }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentTab: 'home',
      tabs: [
        { name: 'home', label: '首页', content: '首页内容' },
        { name: 'about', label: '关于', content: '关于内容' }
      ]
    };
  }
};
</script>

使用 Vue Router 实现 Tab 切换

适合需要 URL 同步的场景,结合路由配置。

vue组件 实现tab切换

// router.js
const routes = [
  { path: '/', component: Home },
  { path: '/about', component: About }
];
<template>
  <div>
    <router-link to="/" custom v-slot="{ navigate }">
      <button @click="navigate" :class="{ active: $route.path === '/' }">
        首页
      </button>
    </router-link>
    <router-link to="/about" custom v-slot="{ navigate }">
      <button @click="navigate" :class="{ active: $route.path === '/about' }">
        关于
      </button>
    </router-link>
    <router-view></router-view>
  </div>
</template>

封装可复用的 Tab 组件

将 Tab 逻辑封装为独立组件,提高复用性。

<!-- Tabs.vue -->
<template>
  <div class="tabs">
    <div class="tab-header">
      <div 
        v-for="tab in tabs" 
        :key="tab.name"
        @click="selectTab(tab)"
        :class="{ 'tab-active': isTabActive(tab) }"
      >
        {{ tab.label }}
      </div>
    </div>
    <div class="tab-content">
      <slot :name="currentTab.name"></slot>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    tabs: Array,
    initialTab: String
  },
  data() {
    return {
      currentTab: this.tabs.find(tab => tab.name === this.initialTab) || this.tabs[0]
    };
  },
  methods: {
    selectTab(tab) {
      this.currentTab = tab;
    },
    isTabActive(tab) {
      return this.currentTab.name === tab.name;
    }
  }
};
</script>

使用第三方库实现

例如 vue-tabs-component 提供开箱即用的解决方案。

npm install vue-tabs-component
<template>
  <tabs>
    <tab name="First tab">
      First tab content
    </tab>
    <tab name="Second tab">
      Second tab content
    </tab>
  </tabs>
</template>

<script>
import { Tabs, Tab } from 'vue-tabs-component';

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

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

相关文章

vue代理实现

vue代理实现

Vue 代理实现方法 在 Vue 项目中,配置代理可以解决开发环境下的跨域问题,通常通过修改 vue.config.js 文件实现。以下是几种常见的代理配置方式: 基础代理配置 在 vue.conf…

vue动画实现

vue动画实现

Vue动画实现方法 Vue提供了多种方式实现动画效果,主要通过内置组件和第三方库完成。 使用<transition>组件 Vue内置的<transition>组件可以为元素添…

vue实现心电图

vue实现心电图

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

vue实现标注

vue实现标注

Vue 实现标注功能的方法 使用 Vue 实现标注功能可以通过多种方式实现,以下介绍几种常见的方法: 1. 使用 HTML5 Canvas 实现标注 Canvas 提供了强大的绘图能力,适合实现复…

vue实现侧导航

vue实现侧导航

Vue 实现侧边导航 使用 Vue 实现侧边导航可以通过多种方式完成,以下是一种常见的实现方法,结合 Vue Router 和动态组件。 基础结构 创建侧边导航栏的基本结构,通常使用 <ul&…

vue实现搜索提示

vue实现搜索提示

Vue实现搜索提示的方法 使用v-model绑定输入框 通过v-model将输入框的值与Vue实例中的数据进行双向绑定,实时获取用户输入内容。 <input v-model="searchQu…