当前位置:首页 > VUE

vue实现tab标签

2026-01-07 00:12:46VUE

Vue 实现 Tab 标签的方法

使用动态组件和 v-for 渲染标签

通过 v-for 循环生成标签页标题,结合 v-bind:class 动态切换激活状态。使用 v-show 或动态组件 <component :is="currentTab"> 控制内容显示。

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

<script>
export default {
  data() {
    return {
      currentTab: 'Home',
      tabs: [
        { title: '首页', name: 'Home' },
        { title: '关于', name: 'About' }
      ]
    }
  },
  components: { Home, About }
}
</script>

<style>
.active {
  background-color: #42b983;
  color: white;
}
</style>

使用 Vue Router 实现路由标签

结合 Vue Router 的嵌套路由或命名视图,通过 <router-link> 实现标签切换,适合需要 URL 同步的场景。

<template>
  <div>
    <router-link 
      v-for="tab in tabs"
      :key="tab.path"
      :to="tab.path"
      active-class="active"
    >
      {{ tab.title }}
    </router-link>
    <router-view></router-view>
  </div>
</template>

使用第三方库

vue-tabs-componentelement-uiel-tabs,可快速实现复杂功能(如动画、懒加载)。

vue实现tab标签

<template>
  <el-tabs v-model="activeTab">
    <el-tab-pane label="首页" name="home">内容1</el-tab-pane>
    <el-tab-pane label="关于" name="about">内容2</el-tab-pane>
  </el-tabs>
</template>

注意事项

  • 动态组件需提前注册子组件。
  • 路由方式需配置对应路由规则。
  • 样式需自定义或配合 UI 框架的类名。

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

相关文章

vue实现自动翻译

vue实现自动翻译

Vue 实现自动翻译的方法 在 Vue 项目中实现自动翻译功能可以通过多种方式完成,以下是几种常见的方法: 使用第三方翻译 API 注册并获取翻译 API 的密钥(如 Google Translat…

vue 实现列表

vue 实现列表

Vue 实现列表的方法 在 Vue 中实现列表通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式: 基础列表渲染 使用 v-for 指令遍历数组,渲染列表项。ite…

vue轮询实现

vue轮询实现

Vue 轮询实现方法 在 Vue 中实现轮询可以通过以下几种方式: 使用 setInterval data() { return { pollInterval: null } },…

vue实现曲线

vue实现曲线

Vue 实现曲线的方法 在 Vue 中实现曲线可以通过多种方式,包括使用 SVG、Canvas 或第三方库如 D3.js、ECharts 等。以下是几种常见的方法: 使用 SVG 绘制曲线 SVG…

vue实现swiper

vue实现swiper

Vue 中实现 Swiper 的方法 安装 Swiper 依赖 在 Vue 项目中安装 Swiper 和相关依赖: npm install swiper vue-awesome-swiper 全局引…

vue 水印 实现

vue 水印 实现

Vue 水印实现方法 使用 canvas 动态生成水印 在 Vue 组件中通过 canvas 绘制水印,并将其作为背景图片插入到目标元素中。这种方法灵活性高,支持动态调整水印内容、角度和透明度。 &…