当前位置:首页 > VUE

vue实现tab标签

2026-01-07 00:12:46VUE

Vue 实现 Tab 标签的方法

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

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

vue实现tab标签

<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 同步的场景。

vue实现tab标签

<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,可快速实现复杂功能(如动画、懒加载)。

<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实现横向导航的方法 使用Flex布局实现横向导航 在Vue中可以通过Flex布局快速实现横向导航。创建一个组件,使用display: flex和flex-direction: row属性使导航项…

vue  select实现

vue select实现

Vue Select 实现方法 在 Vue 中实现下拉选择功能可以使用原生 <select> 标签或第三方库如 vue-select。以下是两种方法的详细说明: 原生 HTML Sele…

vue实现xss

vue实现xss

XSS 攻击简介 XSS(跨站脚本攻击)是一种常见的安全漏洞,攻击者通过注入恶意脚本到网页中,当其他用户访问该页面时,脚本会在其浏览器中执行,可能导致数据泄露或会话劫持。 Vue 中的 XSS…

vue实现选择框

vue实现选择框

Vue 实现选择框的方法 Vue 中可以通过多种方式实现选择框(下拉框),包括原生 HTML 的 <select> 元素结合 Vue 的数据绑定,或者使用第三方 UI 库如 Element…

vue rooter 实现原理

vue rooter 实现原理

Vue Router 实现原理 Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。其核心实现原理包括路由匹配、组件渲染和导航守卫等机制。 路由匹配与动态路由 V…

vue实现数据删除

vue实现数据删除

vue实现数据删除的方法 使用v-for和v-on指令 在Vue中可以通过v-for指令渲染列表数据,结合v-on指令监听删除事件。定义一个方法处理删除逻辑,通常使用splice方法从数组中移除指定项…