当前位置:首页 > VUE

vue 实现点击tab切换

2026-01-23 01:31:45VUE

Vue 实现 Tab 切换的方法

使用 v-showv-if 控制内容显示

通过绑定 v-showv-if 到当前激活的 Tab 索引,动态切换内容显示。v-show 通过 CSS 控制显示/隐藏,适合频繁切换的场景;v-if 会销毁和重建 DOM,适合内容较重的 Tab。

vue 实现点击tab切换

<template>
  <div>
    <div class="tabs">
      <button 
        v-for="(tab, index) in tabs" 
        :key="index"
        @click="activeTab = index"
        :class="{ active: activeTab === index }"
      >
        {{ tab.title }}
      </button>
    </div>
    <div class="content">
      <div v-show="activeTab === 0">{{ tabs[0].content }}</div>
      <div v-show="activeTab === 1">{{ tabs[1].content }}</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeTab: 0,
      tabs: [
        { title: 'Tab 1', content: 'Content for Tab 1' },
        { title: 'Tab 2', content: 'Content for Tab 2' }
      ]
    };
  }
};
</script>

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

使用动态组件 <component :is>

通过动态组件可以更灵活地切换不同组件作为 Tab 内容,适合每个 Tab 内容结构差异较大的场景。

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

<script>
import Tab1Content from './Tab1Content.vue';
import Tab2Content from './Tab2Content.vue';

export default {
  components: { Tab1Content, Tab2Content },
  data() {
    return {
      currentComponent: 'Tab1Content',
      tabs: [
        { title: 'Tab 1', component: 'Tab1Content' },
        { title: 'Tab 2', component: 'Tab2Content' }
      ]
    };
  }
};
</script>

使用 Vue Router 实现路由级 Tab

通过路由参数或路径匹配实现 Tab 切换,适合需要保持浏览器历史记录或分享链接的场景。

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

<script>
export default {
  data() {
    return {
      tabs: [
        { title: 'Tab 1', path: '/tab1' },
        { title: 'Tab 2', path: '/tab2' }
      ]
    };
  }
};
</script>

注意事项

  • 如果 Tab 内容需要保持状态(如表单输入),使用 <keep-alive> 包裹动态组件或路由视图。
  • 对于复杂动画效果,可以结合 Vue 的过渡系统(<transition>)实现平滑切换。
  • 移动端场景可考虑集成第三方库(如 vant 的 Tab 组件)实现手势滑动切换。

vue 实现点击tab切换

标签: vuetab
分享给朋友:

相关文章

vue单页面实现登录

vue单页面实现登录

实现登录功能的基本流程 在Vue单页面应用中实现登录功能,通常需要以下几个关键步骤: 创建登录表单组件 使用Vue的模板语法创建包含用户名和密码输入框的表单,并绑定数据到组件的data属性。 <…

vue实现倒计时抢券

vue实现倒计时抢券

Vue 实现倒计时抢券功能 核心逻辑 倒计时抢券功能需要结合时间计算、状态管理和界面交互。通过 Vue 的响应式特性和生命周期钩子,可以高效实现这一需求。 实现步骤 1. 数据准备 在 Vue 组…

vue实现点击

vue实现点击

实现点击事件的基本方法 在Vue中实现点击事件可以通过v-on指令或@简写方式绑定。以下是常见的点击事件绑定方法: <template> <button v-on:click=…

实现vue组件

实现vue组件

Vue 组件的基本实现 Vue 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。以下是实现 Vue 组件的几种方式: 单文件组件 (SFC) 使用 .vue 文件格式,…

vue 实现tabs

vue 实现tabs

Vue 实现 Tabs 组件的方法 使用动态组件和 v-for 在 Vue 中实现 Tabs 可以通过动态组件和 v-for 指令结合完成。定义一个 tabs 数组,包含每个标签的标题和对应的组件。…

vue实现alert

vue实现alert

实现自定义 Alert 组件 在 Vue 中可以通过组件化方式实现自定义 Alert 弹窗。以下是基于 Vue 3 的实现示例: 组件代码 (Alert.vue) <template>…