当前位置:首页 > VUE

vue实现tab表切换

2026-02-23 23:28:40VUE

Vue 实现 Tab 标签页切换

方法一:使用 v-ifv-show 控制显示

通过 v-ifv-show 动态切换不同标签页内容,结合 v-for 渲染标签按钮。

<template>
  <div>
    <div class="tabs">
      <button 
        v-for="(tab, index) in tabs" 
        :key="index"
        @click="currentTab = index"
        :class="{ active: currentTab === index }"
      >
        {{ tab.name }}
      </button>
    </div>

    <div class="content">
      <div v-if="currentTab === 0">内容1</div>
      <div v-if="currentTab === 1">内容2</div>
      <div v-if="currentTab === 2">内容3</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentTab: 0,
      tabs: [
        { name: '标签1' },
        { name: '标签2' },
        { name: '标签3' }
      ]
    }
  }
}
</script>

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

方法二:动态组件 <component :is>

通过动态组件切换不同子组件,适合复杂标签页内容。

<template>
  <div>
    <div class="tabs">
      <button 
        v-for="(tab, index) in tabs" 
        :key="index"
        @click="currentTab = tab.component"
        :class="{ active: currentTab === tab.component }"
      >
        {{ tab.name }}
      </button>
    </div>

    <component :is="currentTab" />
  </div>
</template>

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

export default {
  components: { Tab1, Tab2 },
  data() {
    return {
      currentTab: 'Tab1',
      tabs: [
        { name: '标签1', component: 'Tab1' },
        { name: '标签2', component: 'Tab2' }
      ]
    }
  }
}
</script>

方法三:使用 Vue Router 实现路由标签页

通过路由切换实现标签页,适合多页面应用。

<template>
  <div>
    <router-link 
      v-for="(tab, index) in tabs" 
      :key="index"
      :to="tab.path"
      active-class="active"
    >
      {{ tab.name }}
    </router-link>

    <router-view />
  </div>
</template>

<script>
export default {
  data() {
    return {
      tabs: [
        { name: '首页', path: '/' },
        { name: '关于', path: '/about' }
      ]
    }
  }
}
</script>

优化技巧

  • 过渡动画:为标签页切换添加 <transition> 效果。

    <transition name="fade" mode="out-in">
      <component :is="currentTab" />
    </transition>
  • 状态保持:使用 <keep-alive> 缓存标签页状态。

    <keep-alive>
      <component :is="currentTab" />
    </keep-alive>
  • 响应式设计:通过 CSS Flex 或 Grid 布局适配移动端。

注意事项

vue实现tab表切换

  • 如果标签页内容较多,优先使用 v-show 避免频繁 DOM 操作。
  • 动态组件需提前注册或在 components 中引入。
  • 路由模式需配置 vue-router 并定义对应路径的组件。

标签: vuetab
分享给朋友:

相关文章

vue 路由 实现

vue 路由 实现

Vue 路由实现方法 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是实现 Vue 路由的详细步骤: 安装 Vue Router 通过 npm 或 ya…

vue实现颜色

vue实现颜色

Vue 实现颜色的方法 在 Vue 中实现颜色管理可以通过多种方式,以下是一些常见的方法: 使用 CSS 变量动态绑定颜色 在 Vue 组件的样式中定义 CSS 变量,并通过 Vue 的数据绑定动态…

vue 实现modal

vue 实现modal

Vue 实现 Modal 的方法 使用 Vue 原生组件 通过 Vue 的组件系统可以轻松实现 Modal。创建一个可复用的 Modal 组件,通过 props 控制显示与隐藏。 <templ…

vue mvvm实现

vue mvvm实现

Vue MVVM 实现原理 Vue.js 是一个基于 MVVM(Model-View-ViewModel)模式的框架,其核心是通过数据绑定和响应式系统实现视图与数据的自动同步。以下是 Vue 实现 M…

vue实现curd

vue实现curd

Vue实现CRUD操作 初始化Vue项目 使用Vue CLI创建新项目,安装必要依赖如axios用于HTTP请求 vue create crud-demo cd crud-demo npm inst…

vue实现表单

vue实现表单

Vue 表单实现方法 双向数据绑定 使用 v-model 指令实现表单元素与数据的双向绑定。适用于 input、textarea、select 等元素。 <template> <…