19. TagEllipsis 关键字转 tag 标签
功能说明
TagEllipsis 组件用于将关键字或字符串数组转换为 Tag 标签展示,支持自动省略和悬浮展开功能。当标签数量超过指定限制时,会自动折叠显示省略号,鼠标悬停可查看完整列表。
引入方式
<script setup>
import TagEllipsis from '@/components/Common/TagEllipsis/TagEllipsis.vue';
</script>
<template>
<TagEllipsis :list="tagList" :limit="2" />
</template>Props 属性
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| list | 需要转成标签的值,支持数组 ['1','2'] 或对象数组 [{name: '标签1'}] 或逗号分隔字符串 '1,2' | Array | String | [] |
| limit | 限制显示多少个元素后使用省略号 | String | Number | 2 |
| defaultProps | list 为对象数组时,用于显示文字的字段名 | String | 'name' |
| emptyText | 空数据时显示的文本内容,设为 false 则不显示 | Boolean | String | - |
Events 事件
| 事件名 | 说明 | 回调参数 |
|---|---|---|
| - | 当前组件无自定义事件 | - |
使用示例
基础用法 - 字符串数组
<template>
<TagEllipsis :list="['标签1', '标签2', '标签3']" :limit="2" />
</template>基础用法 - 逗号分隔字符串
<template>
<TagEllipsis :list="'标签1,标签2,标签3'" :limit="2" />
</template>对象数组 - 自定义字段
<template>
<TagEllipsis
:list="[{ name: '红色', color: 'red' }, { name: '蓝色', color: 'blue' }]"
:defaultProps="'name'"
:limit="1"
/>
</template>自定义空状态
<template>
<TagEllipsis :list="[]" emptyText="暂无标签" />
<TagEllipsis :list="[]" :emptyText="false" />
</template>配合 a-tag 属性使用
<template>
<!-- 所有 attrs 会透传给 a-tag 组件 -->
<TagEllipsis
:list="tagList"
color="blue"
:limit="2"
/>
</template>源码路径
Widiazine-New-Frontend/src/components/Common/TagEllipsis/
├── TagEllipsis.vue # 主组件注意事项
defaultProps参数仅在list为对象数组时生效- 当使用对象数组时,确保每个对象都有
defaultProps指定的字段 - 组件会自动过滤空值(空字符串或字段不存在的对象)
limit属性控制显示的标签数量,超出部分在 Popover 中展示