Appearance
Tag 标签
进行标记和分类的小标签。
代码演示
基础用法
基本标签的用法,可以通过添加 closable 变为可关闭标签。可关闭标签具有 close 两个事件。
vue
<template>
<div>
<a-tag>Tag 1</a-tag>
<a-tag><a href="https://github.com/vueComponent/ant-design">Link</a></a-tag>
<a-tag closable @close="log">Tag 2</a-tag>
<a-tag closable @close.prevent>Prevent Default</a-tag>
</div>
</template>
<script setup>
const log = e => {
console.log(e);
};
</script>可选择标签
可通过 CheckableTag 实现类似 Checkbox 的效果,点击切换选中效果。
Categories:
Movies
Books
Music
Sports
vue
<template>
<span style="margin-right: 8px">Categories:</span>
<a-space :size="[0, 8]" wrap>
<a-checkable-tag
v-for="(tag, index) in tagsData"
:key="tag"
v-model:checked="selectTags[index]"
@change="checked => handleChange(tag, checked)"
>
{{ tag }}
</a-checkable-tag>
</a-space>
</template>
<script setup>
import { reactive } from 'vue';
const tagsData = reactive(['Movies', 'Books', 'Music', 'Sports']);
const selectTags = reactive([false, true, false, false]);
const handleChange = (tag, checked) => {
console.log(tag, checked);
};
</script>多彩标签
我们添加了多种预设色彩的标签样式,用作不同场景使用。如果预设值不能满足你的需求,可以设置为具体的色值。
Presets:
pinkredorangegreencyanbluepurple
Custom:
#f50#2db7f5#87d068#108ee9
vue
<template>
<h4 style="margin-bottom: 16px">Presets:</h4>
<div>
<a-tag color="pink">pink</a-tag>
<a-tag color="red">red</a-tag>
<a-tag color="orange">orange</a-tag>
<a-tag color="green">green</a-tag>
<a-tag color="cyan">cyan</a-tag>
<a-tag color="blue">blue</a-tag>
<a-tag color="purple">purple</a-tag>
</div>
<h4 style="margin: 16px 0">Custom:</h4>
<div>
<a-tag color="#f50">#f50</a-tag>
<a-tag color="#2db7f5">#2db7f5</a-tag>
<a-tag color="#87d068">#87d068</a-tag>
<a-tag color="#108ee9">#108ee9</a-tag>
</div>
</template>动态添加和删除
用数组生成一组标签,可以动态添加和删除。
UnremovableTag 2Tag 3Tag 3Tag 3Tag 3... New Tagvue
<template>
<template v-for="(tag, index) in state.tags" :key="tag">
<a-tooltip v-if="tag.length > 20" :title="tag">
<a-tag :closable="index !== 0" @close="handleClose(tag)">
{{ `${tag.slice(0, 20)}...` }}
</a-tag>
</a-tooltip>
<a-tag v-else :closable="index !== 0" @close="handleClose(tag)">
{{ tag }}
</a-tag>
</template>
<a-input
v-if="state.inputVisible"
ref="inputRef"
v-model:value="state.inputValue"
type="text"
size="small"
:style="{ width: '78px' }"
@blur="handleInputConfirm"
@keyup.enter="handleInputConfirm"
/>
<a-tag v-else style="background: #fff; border-style: dashed" @click="showInput">
New Tag
</a-tag>
</template>
<script setup>
import { ref, reactive, nextTick } from 'vue';
const inputRef = ref();
const state = reactive({
tags: ['Unremovable', 'Tag 2', 'Tag 3Tag 3Tag 3Tag 3Tag 3Tag 3Tag 3'],
inputVisible: false,
inputValue: '',
});
const handleClose = removedTag => {
const tags = state.tags.filter(tag => tag !== removedTag);
console.log(tags);
state.tags = tags;
};
const showInput = () => {
state.inputVisible = true;
nextTick(() => {
inputRef.value.focus();
});
};
const handleInputConfirm = () => {
const inputValue = state.inputValue;
let tags = state.tags;
if (inputValue && tags.indexOf(inputValue) === -1) {
tags = [...tags, inputValue];
}
console.log(tags);
Object.assign(state, {
tags,
inputVisible: false,
inputValue: '',
});
};
</script>预设状态的标签
预设五种状态颜色,可以通过设置 color 为 success、 processing、error、default、warning 来代表不同的状态。
Without icon
successprocessingerrorwarningdefault
With icon
success processing error warning waiting stop
vue
<template>
<a-divider orientation="left">Without icon</a-divider>
<div>
<a-tag color="success">success</a-tag>
<a-tag color="processing">processing</a-tag>
<a-tag color="error">error</a-tag>
<a-tag color="warning">warning</a-tag>
<a-tag color="default">default</a-tag>
</div>
<a-divider orientation="left">With icon</a-divider>
<div>
<a-tag color="success">
success
</a-tag>
<a-tag color="processing">
processing
</a-tag>
<a-tag color="error">
error
</a-tag>
<a-tag color="warning">
warning
</a-tag>
<a-tag color="default">
waiting
</a-tag>
<a-tag color="default">
stop
</a-tag>
</div>
</template>
<script setup>
export {};
</script>无边框
无边框模式。
Tag 1
Tag 2
Tag 3
Tag 4
processing
success
error
warning
magenta
red
volcano
orange
gold
lime
green
cyan
blue
geekblue
purple
vue
<template>
<a-space :size="[0, 'small']" wrap>
<a-tag :bordered="false">Tag 1</a-tag>
<a-tag :bordered="false">Tag 2</a-tag>
<a-tag :bordered="false" closable>Tag 3</a-tag>
<a-tag :bordered="false" closable>Tag 4</a-tag>
</a-space>
<a-divider />
<a-space :size="[0, 'small']" wrap>
<a-tag :bordered="false" color="processing">processing</a-tag>
<a-tag :bordered="false" color="success">success</a-tag>
<a-tag :bordered="false" color="error">error</a-tag>
<a-tag :bordered="false" color="warning">warning</a-tag>
<a-tag :bordered="false" color="magenta">magenta</a-tag>
<a-tag :bordered="false" color="red">red</a-tag>
<a-tag :bordered="false" color="volcano">volcano</a-tag>
<a-tag :bordered="false" color="orange">orange</a-tag>
<a-tag :bordered="false" color="gold">gold</a-tag>
<a-tag :bordered="false" color="lime">lime</a-tag>
<a-tag :bordered="false" color="green">green</a-tag>
<a-tag :bordered="false" color="cyan">cyan</a-tag>
<a-tag :bordered="false" color="blue">blue</a-tag>
<a-tag :bordered="false" color="geekblue">geekblue</a-tag>
<a-tag :bordered="false" color="purple">purple</a-tag>
</a-space>
</template>API
Tag
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| closable | 标签是否可以关闭 | boolean | false |
| closeIcon | 自定义关闭按钮 | VNode | slot | - |
| color | 标签色 | string | - |
| icon | 设置图标 | VNode | slot | - |
| bordered | 是否有边框 | boolean | true |
事件
| 事件名称 | 说明 | 回调参数 |
|---|---|---|
| close | 关闭时的回调 | (e) => void |
Tag.CheckableTag
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| checked(v-model) | 设置标签的选中状态 | boolean | false |
事件
| 事件名称 | 说明 | 回调参数 |
|---|---|---|
| change | 点击标签时触发的回调 | (checked) => void |

