Tabs标签页
Tabs 标签页
选项卡切换组件。
何时使用
提供平级的区域将大块内容进行收纳和展现,保持界面整洁。
Ant Design 依次提供了三级选项卡,分别用于不同的场景。
-
卡片式的页签,提供可关闭的样式,常用于容器顶部。
-
标准线条式页签,用于容器内部的主功能切换,这是最常用的 Tabs。
-
RadioButton 可作为更次级的页签来使用。
代码演示
基本用法
默认选中第一项。
<template> <a-tabs v-model:activeKey="activeKey"> <a-tab-pane key="1" tab="Tab 1">Content of Tab Pane 1</a-tab-pane> <a-tab-pane key="2" tab="Tab 2" force-render>Content of Tab Pane 2</a-tab-pane> <a-tab-pane key="3" tab="Tab 3">Content of Tab Pane 3</a-tab-pane> </a-tabs></template><script lang="ts">import { defineComponent, ref } from 'vue';export default defineComponent({ setup() { return { activeKey: ref('1'), }; },});</script>
禁用
禁用某一项。
<template> <a-tabs v-model:activeKey="activeKey"> <a-tab-pane key="1" tab="Tab 1">Tab 1</a-tab-pane> <a-tab-pane key="2" tab="Tab 2" disabled>Tab 2</a-tab-pane> <a-tab-pane key="3" tab="Tab 3">Tab 3</a-tab-pane> </a-tabs></template><script lang="ts">import { defineComponent, ref } from 'vue';export default defineComponent({ setup() { return { activeKey: ref('1'), }; },});</script>
图标
有图标的标签。
<template> <a-tabs v-model:activeKey="activeKey"> <a-tab-pane key="1"> <template #tab> <span> <apple-outlined /> Tab 1 </span> </template> Tab 1 </a-tab-pane> <a-tab-pane key="2"> <template #tab> <span> <android-outlined /> Tab 2 </span> </template> Tab 2 </a-tab-pane> </a-tabs></template><script lang="ts">import { AppleOutlined, AndroidOutlined } from '@ant-design/icons-vue';import { defineComponent, ref } from 'vue';export default defineComponent({ setup() { return { activeKey: ref('1'), }; }, components: { AppleOutlined, AndroidOutlined, },});</script>
滑动
可以左右、上下滑动,容纳更多标签。
<template> <div style="width: 500px"> <a-radio-group v-model:value="mode" :style="{ marginBottom: '8px' }"> <a-radio-button value="top">Horizontal</a-radio-button> <a-radio-button value="left">Vertical</a-radio-button> </a-radio-group> <a-tabs :tab-position="mode" :style="{ height: '200px' }" @prevClick="callback" @nextClick="callback" v-model:activeKey="activeKey" > <a-tab-pane v-for="i in 30" :key="i" :tab="`Tab-${i}`">Content of tab {{ i }}</a-tab-pane> </a-tabs> </div></template><script lang="ts">import { defineComponent, ref } from 'vue';export default defineComponent({ setup() { const mode = ref('top'); const activeKey = ref('1'); const callback = (val: string) => { console.log(val); }; return { mode, callback, activeKey, }; },});</script>
附加内容
可以在页签右边添加附加操作。
<template> <a-tabs v-model:activeKey="activeKey"> <a-tab-pane key="1" tab="Tab 1">Content of tab 1</a-tab-pane> <a-tab-pane key="2" tab="Tab 2">Content of tab 2</a-tab-pane> <a-tab-pane key="3" tab="Tab 3">Content of tab 3</a-tab-pane> <template #tabBarExtraContent> <a-button>Extra Action</a-button> </template> </a-tabs></template><script lang="ts">import { defineComponent, ref } from 'vue';export default defineComponent({ setup() { return { activeKey: ref('1'), }; },});</script>
大小
大号页签用在页头区域,小号用在弹出框等较狭窄的容器内。
<template> <div> <a-radio-group v-model:value="size" style="margin-bottom: 16px"> <a-radio-button value="small">Small</a-radio-button> <a-radio-button value="default">Default</a-radio-button> <a-radio-button value="large">Large</a-radio-button> </a-radio-group> <a-tabs v-model:activeKey="activeKey" :size="size"> <a-tab-pane key="1" tab="Tab 1">Content of tab 1</a-tab-pane> <a-tab-pane key="2" tab="Tab 2">Content of tab 2</a-tab-pane> <a-tab-pane key="3" tab="Tab 3">Content of tab 3</a-tab-pane> </a-tabs> </div></template><script lang="ts">import { defineComponent, ref } from 'vue';export default defineComponent({ setup() { const size = ref('small'); const activeKey = ref('1'); return { size, activeKey, }; },});</script>
位置
有四个位置,tabPosition="left|right|top|bottom"。
<template> <div style="width: 500px"> <a-radio-group v-model:value="tabPosition" style="margin: 8px"> <a-radio-button value="top">top</a-radio-button> <a-radio-button value="bottom">bottom</a-radio-button> <a-radio-button value="left">left</a-radio-button> <a-radio-button value="right">right</a-radio-button> </a-radio-group> <a-tabs v-model:activeKey="activeKey" :tab-position="tabPosition"> <a-tab-pane key="1" tab="Tab 1">Content of Tab 1</a-tab-pane> <a-tab-pane key="2" tab="Tab 2">Content of Tab 2</a-tab-pane> <a-tab-pane key="3" tab="Tab 3">Content of Tab 3</a-tab-pane> </a-tabs> </div></template><script lang="ts">import { defineComponent, ref } from 'vue';export default defineComponent({ setup() { const tabPosition = ref('top'); const activeKey = ref('1'); return { tabPosition, activeKey, }; },});</script>
卡片式页签
另一种样式的页签,不提供对应的垂直样式。
<template> <a-tabs type="card" v-model:activeKey="activeKey"> <a-tab-pane key="1" tab="Tab 1">Content of Tab Pane 1</a-tab-pane> <a-tab-pane key="2" tab="Tab 2">Content of Tab Pane 2</a-tab-pane> <a-tab-pane key="3" tab="Tab 3">Content of Tab Pane 3</a-tab-pane> </a-tabs></template><script lang="ts">import { defineComponent, ref } from 'vue';export default defineComponent({ setup() { return { activeKey: ref('1'), }; },});</script>
新增和关闭页签
只有卡片样式的页签支持新增和关闭选项。 使用 :closable="false" 禁止关闭。
<template> <a-tabs v-model:activeKey="activeKey" type="editable-card" @edit="onEdit"> <a-tab-pane v-for="pane in panes" :key="pane.key" :tab="pane.title" :closable="pane.closable"> {{ pane.content }} </a-tab-pane> </a-tabs></template><script lang="ts">import { defineComponent, ref } from 'vue';export default defineComponent({ setup() { const panes = ref([ { title: 'Tab 1', content: 'Content of Tab 1', key: '1' }, { title: 'Tab 2', content: 'Content of Tab 2', key: '2' }, { title: 'Tab 3', content: 'Content of Tab 3', key: '3', closable: false }, ]); const activeKey = ref(panes.value[0].key); const newTabIndex = ref(0); const callback = (key: string) => { console.log(key); }; const add = () => { activeKey.value = `newTab${++newTabIndex.value}`; panes.value.push({ title: 'New Tab', content: 'Content of new Tab', key: activeKey.value }); }; const remove = (targetKey: string) => { let lastIndex = 0; panes.value.forEach((pane, i) => { if (pane.key === targetKey) { lastIndex = i - 1; } }); panes.value = panes.value.filter(pane => pane.key !== targetKey); if (panes.value.length && activeKey.value === targetKey) { if (lastIndex >= 0) { activeKey.value = panes.value[lastIndex].key; } else { activeKey.value = panes.value[0].key; } } }; const onEdit = (targetKey: string | MouseEvent, action: string) => { if (action === 'add') { add(); } else { remove(targetKey as string); } }; return { panes, activeKey, callback, onEdit, }; },});</script>
卡片式页签容器
用于容器顶部,需要一点额外的样式覆盖。
