当前位置:首页 > VUE

vue tab实现例子

2026-01-18 18:25:11VUE

Vue Tab 实现示例

以下是一个基于 Vue.js 的标签页(Tab)组件的实现示例,包含动态切换和基础样式。

基础模板结构

<template>
  <div class="tabs">
    <div class="tab-header">
      <button 
        v-for="(tab, index) in tabs" 
        :key="index"
        @click="activeTab = index"
        :class="{ 'active': activeTab === index }"
      >
        {{ tab.title }}
      </button>
    </div>
    <div class="tab-content">
      <div v-for="(tab, index) in tabs" :key="index" v-show="activeTab === index">
        {{ tab.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' },
        { title: 'Tab 3', content: 'Content for Tab 3' }
      ]
    };
  }
};
</script>

样式部分

<style scoped>
.tabs {
  font-family: Arial, sans-serif;
  max-width: 600px;
  margin: 0 auto;
}

.tab-header {
  display: flex;
  border-bottom: 1px solid #ddd;
}

.tab-header button {
  padding: 10px 20px;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 16px;
}

.tab-header button.active {
  border-bottom: 2px solid #42b983;
  color: #42b983;
}

.tab-content {
  padding: 20px;
  min-height: 100px;
}
</style>

动态组件实现

如果需要更复杂的动态内容,可以使用 Vue 的动态组件:

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

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

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

使用第三方库

对于更复杂的需求,可以考虑使用现成的 Vue Tab 组件库:

vue tab实现例子

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

相关文章

vue前端分页怎么实现

vue前端分页怎么实现

前端分页实现方法 在Vue中实现前端分页通常涉及数据分割和页面控制逻辑。以下是常见的实现方式: 使用计算属性分页 通过计算属性对数据进行切片处理: computed: { paginatedD…

vue实现倒计时抢券

vue实现倒计时抢券

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

vue 实现复选

vue 实现复选

Vue 实现复选功能 在 Vue 中实现复选功能可以通过多种方式完成,以下是常见的几种方法: 使用 v-model 绑定数组 通过 v-model 绑定一个数组,可以自动收集用户选择的复选框值。…

vue  select实现

vue select实现

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

vue实现https

vue实现https

Vue 中实现 HTTPS 的步骤 在 Vue 项目中实现 HTTPS 主要涉及开发环境配置和生产环境部署。以下是具体方法: 开发环境配置(Vue CLI 或 Vite) 使用 mkcert 生成…

vue 实现豆瓣

vue 实现豆瓣

以下是基于 Vue 实现豆瓣电影类功能的实现方案,涵盖核心模块和技术要点: 数据获取与 API 调用 使用豆瓣开放 API(需注意调用频率限制)或第三方代理接口 推荐 axios 进行异步请求,配合…