Tabs标签页·续1
<template> <div class="card-container"> <a-tabs v-model:activeKey="activeKey" type="card"> <a-tab-pane key="1" tab="Tab Title 1"> <p>Content of Tab Pane 1</p> <p>Content of Tab Pane 1</p> <p>Content of Tab Pane 1</p> </a-tab-pane> <a-tab-pane key="2" tab="Tab Title 2"> <p>Content of Tab Pane 2</p> <p>Content of Tab Pane 2</p> <p>Content of Tab Pane 2</p> </a-tab-pane> <a-tab-pane key="3" tab="Tab Title 3"> <p>Content of Tab Pane 3</p> <p>Content of Tab Pane 3</p> <p>Content of Tab Pane 3</p> </a-tab-pane> </a-tabs> </div></template><script lang="ts">import { defineComponent, ref } from 'vue';export default defineComponent({ setup() { return { activeKey: ref(1), }; },});</script><style>.card-container { background: #f5f5f5; overflow: hidden; padding: 24px;}.card-container > .ant-tabs-card > .ant-tabs-content { height: 120px; margin-top: -16px;}.card-container > .ant-tabs-card > .ant-tabs-content > .ant-tabs-tabpane { background: #fff; padding: 16px;}.card-container > .ant-tabs-card > .ant-tabs-bar { border-color: #fff;}.card-container > .ant-tabs-card > .ant-tabs-bar .ant-tabs-tab { border-color: transparent; background: transparent;}.card-container > .ant-tabs-card > .ant-tabs-bar .ant-tabs-tab-active { border-color: #fff; background: #fff;}</style>
自定义新增页签触发器
隐藏默认的页签增加图标,给自定义触发器绑定事件。
<template> <div> <div :style="{ marginBottom: '16px' }"> <a-button @click="add">ADD</a-button> </div> <a-tabs v-model:activeKey="activeKey" hide-add 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> </div></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' }, ]); const activeKey = ref(panes.value[0].key); const newTabIndex = ref(0); const add = () => { activeKey.value = `newTab${newTabIndex.value++}`; panes.value.push({ title: `New Tab ${activeKey.value}`, content: `Content of new Tab ${activeKey.value}`, 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) => { remove(targetKey); }; return { panes, activeKey, onEdit, add, }; },});</script>
自定义页签头
自定义页签头
<template> <div> <a-tabs v-model:activeKey="activeKey"> <a-tab-pane key="1" tab="Tab 1" style="height: 200px">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> <template #renderTabBar="{ DefaultTabBar, ...props }"> <component :is="DefaultTabBar" v-bind="props" :style="{ zIndex: 1, background: '#fff', textAlign: 'right' }" /> </template> </a-tabs> </div></template><script lang="ts">import { defineComponent, ref } from 'vue';export default defineComponent({ setup() { return { activeKey: ref('1'), }; },});</script>
API
Tabs
| activeKey(v-model) |
当前激活 tab 面板的 key |
string |
无 |
|
|
|
|
| animated | 是否使用动画切换 Tabs,在 tabPosition=top | bottom 时有效 | boolean | {inkBar:boolean, tabPane:boolean} | true, 当 type=”card” 时为 false |
|---|---|---|---|
| defaultActiveKey |
初始化选中面板的 key,如果没有设置 activeKey |
string |
第一个面板 |
|
|
|
|
| hideAdd |
是否隐藏加号图标,在 type=”editable-card” 时有效 |
boolean |
false |
|
|
|
|
| size |
大小,提供 large default 和 small 三种大小 |
string |
‘default’ |
|
|
|
|
| tabBarExtraContent |
tab bar 上额外的元素 |
slot |
无 |
|
|
|
|
| tabBarStyle |
tab bar 的样式对象 |
object |
- |
|
|
|
|
| tabPosition |
页签位置,可选值有 top right bottom left |
string |
‘top’ |
|
|
|
|
| type |
页签的基本样式,可选 line、card editable-card 类型 |
string |
‘line’ |
|
|
|
|
| tabBarGutter |
tabs 之间的间隙 |
number |
无 |
|
|
|
|
事件
| change |
切换面板的回调 |
Function(activeKey) {} |
|
|
|
| edit |
新增和删除页签的回调,在 type=”editable-card” 时有效 |
(targetKey, action): void |
|
|
|
| nextClick |
next 按钮被点击的回调 |
Function |
|
|
|
| prevClick |
prev 按钮被点击的回调 |
Function |
|
|
|
| tabClick |
tab 被点击的回调 |
Function |
|
|
|
Tabs.TabPane
| forceRender |
被隐藏时是否渲染 DOM 结构 |
boolean |
false |
|
|
|
|
| key |
对应 activeKey |
string |
无 |
|
|
|
|
| tab | 选项卡头显示文字 | string|slot | 无 |
|---|---|---|---|