Skip to content

Tabs 标签页

介绍

标签页组件,用于在不同的内容区域之间进行切换。

导入

js
import Tabs from '@/components/nav/Tabs.vue';
import TabsPage from '@/components/nav/TabsPage.vue';

使用方法

基础用法

vue
<template>
  <Tabs
    :tabs="[
      { text: '标签页1' },
      { text: '标签页2' },
      { text: '标签页3' },
    ]"
    v-model:currentIndex="tabIndex"
  />
</template>

<script setup>
import { ref } from 'vue';
import Tabs from '@/components/nav/Tabs.vue';

const tabIndex = ref(0);
</script>

自定义标签宽度

设置 autoItemWidth=false 禁用标签自动大小,此时可通过 defaultItemWidth 设置所有标签宽度,也可通过条目的 width 单独设置宽度。

vue
<template>
  <Tabs
    :tabs="[
      { text: '正常标签页' },
      { text: '自定义指示器宽度', width: 300, indicatorWidth: 300 },
      { text: '标签页3 宽度400', width: 400 },
      { text: '标签页4 宽度200', width: 200 },
    ]"
    :autoItemWidth="false"
    :defaultItemWidth="200"
    v-model:currentIndex="tabIndex"
  />
</template>

<script setup>
import { ref } from 'vue';
import Tabs from '@/components/nav/Tabs.vue';

const tabIndex = ref(0);
</script>

标签栏滚动

设置 autoItemWidth=false 禁用标签自动大小,当标签宽度超出父级时,标签栏可以在水平方向上滚动,切换时会自动将当前标签居中。

vue
<template>
  <Tabs
    :tabs="[
      { text: '标签页1' },
      { text: '标签页2' },
      { text: '标签页3' },
      { text: '标签页4' },
      { text: '标签页5' },
      { text: '标签页6' },
      { text: '标签页7' },
      { text: '标签页8' },
      { text: '标签页9' },
      { text: '标签页10' },
    ]"
    :autoItemWidth="false"
    :defaultItemWidth="200"
    v-model:currentIndex="tabIndex"
  />
</template>

<script setup>
import { ref } from 'vue';
import Tabs from '@/components/nav/Tabs.vue';

const tabIndex = ref(0);
</script>

禁用标签

设置 disabled 属性即可禁用标签。

vue
<template>
  <Tabs
    :tabs="[
      { text: '标签页1' },
      { text: '标签页2' },
      { text: '标签页3', disabled: true },
      { text: '标签页4', disabled: true },
      { text: '标签页5' },
    ]"
    v-model:currentIndex="tabIndex"
  />
</template>

<script setup>
import { ref } from 'vue';
import Tabs from '@/components/nav/Tabs.vue';

const tabIndex = ref(0);
</script>

带小红点

通过 badgeProps 属性可以在标签上添加徽标。

vue
<template>
  <Tabs
    :tabs="[
      { text: '标签页1' },
      { text: '带小红点', badgeProps: { content: '', offset: { x: -8, y: 2 } } },
      { text: '带数字红点', badgeProps: { content: 423, maxCount: 99, offset: { x: 8, y: 2 } } },
    ]"
    v-model:currentIndex="tabIndex"
  />
</template>

<script setup>
import { ref } from 'vue';
import Tabs from '@/components/nav/Tabs.vue';

const tabIndex = ref(0);
</script>

自定义渲染

通过 tab 插槽可以自定义标签的内容和样式。

vue
<template>
  <Tabs
    :tabs="[
      { text: '标签页1' },
      { text: '标签页2' },
    ]"
    v-model:currentIndex="tabIndex"
  >
    <template #tab="{ tab, active, index, width }">
      <FlexRow center :width="width">
        <Icon icon="warning" />
        <Text :color="active ? 'danger' : 'success'">{{tab.text}}</Text>
      </FlexRow>
    </template>
  </Tabs>
</template>

<script setup>
import { ref } from 'vue';
import Tabs from '@/components/nav/Tabs.vue';
import FlexRow from '@/components/layout/FlexRow.vue';
import Icon from '@/components/basic/Icon.vue';
import Text from '@/components/basic/Text.vue';

const tabIndex = ref(0);
</script>

标签页内容组件

使用 TabsPage 组件可以方便地管理标签页内容的显示与切换。

vue
<template>
  <TabsPage
    :tabs="[
      { text: '标签页0' },
      { text: '标签页1', badgeProps: { offset: { x: 8, y: 2 } } },
      { text: '标签页2', badgeProps: { content: 423, maxCount: 99, offset: { x: 8, y: 2 } } },
    ]"
  >
    <template #page="{ key }">
      <FlexCol height="400rpx" :flex="1" center>
        <Text>标签页内容{{ key }}</Text>
      </FlexCol>
    </template>
  </TabsPage>
</template>

<script setup>
import TabsPage from '@/components/nav/TabsPage.vue';
import FlexCol from '@/components/layout/FlexCol.vue';
import Text from '@/components/basic/Text.vue';
</script>

API 参考

Tabs

Props

名称说明类型必填默认值
tabs标签数据TabsItemData[][]
v-model:currentIndex当前选中的标签位置number0
height整个组件的高度number90
width整个组件的宽度number750
autoItemWidth是否自动根据容器宽度调整标签宽度booleantrue
indicatorAnim指示器切换标签时是否有动画效果booleantrue
autoScroll是否在用户切换标签后自动滚动至当前选中的条目booleantrue
defaultItemWidth标签宽度,在 autoItemWidth 为 false 时有效number120
defaultIndicatorWidth默认的指示器宽度number100
itemStyle标签自定义样式object{}
textColor标签正常状态的文字颜色string'text.title'
disableTextColor标签禁用状态的文字颜色string'grey'
textStyle标签正常状态的文字样式object{}
activeTextColor标签激活状态的文字颜色string'primary'
activeTextStyle标签激活状态的文字样式object{}
underlayColor标签按下颜色string'pressed.white'
indicatorStyle指示器自定义样式object{}
showIndicator显示指示器booleantrue
innerStyle组件内部容器样式object{}

Events

名称说明参数
update:currentIndex标签切换时触发currentIndex: number

Slots

名称说明参数
tab自定义标签内容{ tab: TabsItemData, index: number, width: number, active: boolean, onClick: () => void }

TabsItemData

名称说明类型必填默认值
text标签显示文字string-
data自定义数据anyundefined
visible是否显示booleantrue
disabled是否禁用选择booleanfalse
badgeProps红点标记的自定义属性,同 Badge 组件object{}
width标签宽度numberundefined
indicatorWidth指示器宽度numberundefined
lazy懒加载,仅在TabsPage组件中有效booleanundefined

主题变量

名称类型默认值说明
TabsDefaultHeightnumber90标签栏默认高度
TabsDefaultWidthnumber750标签栏默认宽度
TabsDefaultItemWidthnumber120标签默认宽度
TabsDefaultIndicatorWidthnumber100指示器默认宽度
TabsTextColorstring'text.title'标签正常状态文字颜色
TabsDisableTextColorstring'grey'标签禁用状态文字颜色
TabsActiveTextColorstring'primary'标签激活状态文字颜色
TabsUnderlayColorstring'pressed.white'标签按下颜色
TabsItemPaddingHorizontalnumber10标签水平内边距