Skip to content

Tag 标签 ​

17:06

说明 ​

用于标记与选择的小标签。内置五种主题色,支持 light / dark / plain 三种模式、square / round / roundLeft / roundRight 四种形状与 default / mini 两档尺寸;closeable 时右侧出现关闭图标并派发 close,disabled 时置灰且不派发点击。文字用 text 传入,也可用默认插槽自定义内容。

兼容性 ​

平台支持情况
App⚠️ 部分支持(根节点为 display: flex,未放入 flex 容器时标签会独占一行)
H5✅ 支持
微信小程序✅ 支持

示例 ​

基础用法 ​

通过 type 参数设置主题类型,默认为 primary。

查看源码
vue
<template>
    <view class="tag-row">
        <ux-tag text="primary" type="primary" custom-class="tag-spacing"></ux-tag>
        <ux-tag text="success" type="success" custom-class="tag-spacing"></ux-tag>
        <ux-tag text="warning" type="warning" custom-class="tag-spacing"></ux-tag>
        <ux-tag text="error" type="error" custom-class="tag-spacing"></ux-tag>
        <ux-tag text="info" type="info" custom-class="tag-spacing"></ux-tag>
    </view>
</template>

<style scoped>
.tag-row {
    display: flex;
    flex-direction: row;
    align-items: center;
    flex-wrap: wrap;
}
.tag-spacing {
    margin-right: 10px;
    margin-bottom: 8px;
}
</style>

设置模式 mode ​

dark(深色背景)、light(浅色背景)、plain(白色背景)。

查看源码
vue
<template>
    <view class="tag-row">
        <ux-tag text="深色" mode="dark" custom-class="tag-spacing"></ux-tag>
        <ux-tag text="浅色" mode="light" custom-class="tag-spacing"></ux-tag>
        <ux-tag text="镂空" mode="plain" custom-class="tag-spacing"></ux-tag>
    </view>
</template>

<style scoped>
.tag-row {
    display: flex;
    flex-direction: row;
    align-items: center;
    flex-wrap: wrap;
}
.tag-spacing {
    margin-right: 10px;
    margin-bottom: 8px;
}
</style>

设置形状 shape ​

square(小圆角 3px,默认)、round(胶囊)、roundLeft(左圆角)、roundRight(右圆角)。

查看源码
vue
<template>
    <view class="tag-row">
        <ux-tag text="小圆角" shape="square" custom-class="tag-spacing"></ux-tag>
        <ux-tag text="胶囊" shape="round" custom-class="tag-spacing"></ux-tag>
        <ux-tag text="左圆角" shape="roundLeft" custom-class="tag-spacing"></ux-tag>
        <ux-tag text="右圆角" shape="roundRight" custom-class="tag-spacing"></ux-tag>
    </view>
</template>

<style scoped>
.tag-row {
    display: flex;
    flex-direction: row;
    align-items: center;
    flex-wrap: wrap;
}
.tag-spacing {
    margin-right: 10px;
    margin-bottom: 8px;
}
</style>

设置大小 size ​

default(默认)、mini(迷你)。

查看源码
vue
<template>
    <view class="tag-row">
        <ux-tag text="默认大小" custom-class="tag-spacing"></ux-tag>
        <ux-tag text="迷你大小" size="mini" custom-class="tag-spacing"></ux-tag>
    </view>
</template>

<style scoped>
.tag-row {
    display: flex;
    flex-direction: row;
    align-items: center;
    flex-wrap: wrap;
}
.tag-spacing {
    margin-right: 10px;
    margin-bottom: 8px;
}
</style>

自定义颜色 ​

通过 color、bg-color、border-color 自定义文字、背景和边框颜色。

查看源码
vue
<template>
    <view class="tag-row">
        <ux-tag text="自定义文字" color="#ff00ff" custom-class="tag-spacing"></ux-tag>
        <ux-tag text="自定义背景" bg-color="#2979ff" color="#ffffff" custom-class="tag-spacing"></ux-tag>
        <ux-tag text="自定义边框" mode="plain" color="#fa3534" border-color="#fa3534" custom-class="tag-spacing"></ux-tag>
    </view>
</template>

<style scoped>
.tag-row {
    display: flex;
    flex-direction: row;
    align-items: center;
    flex-wrap: wrap;
}
.tag-spacing {
    margin-right: 10px;
    margin-bottom: 8px;
}
</style>

可关闭 closeable ​

设置 closeable 为 true,点击关闭按钮会触发 close 事件。

查看源码
vue
<template>
    <view class="tag-row">
        <ux-tag text="可关闭标签" closeable close-color="#fa3534" :show="show" @close="onClose"></ux-tag>
        <ux-tag text="深色可关闭" closeable mode="dark" :show="show2" @close="onClose2"></ux-tag>
    </view>
</template>

<script setup lang="uts">
const show = ref<boolean>(true);
const show2 = ref<boolean>(true);

function onClose(): void {
    show.value = false;
}

function onClose2(): void {
    show2.value = false;
}
</script>

<style scoped>
.tag-row {
    display: flex;
    flex-direction: row;
    align-items: center;
    flex-wrap: wrap;
}
</style>

禁用 disabled ​

设置 disabled 为 true,标签变灰且不可点击。

查看源码
vue
<template>
    <view class="tag-row">
        <ux-tag text="禁用标签" disabled custom-class="tag-spacing"></ux-tag>
        <ux-tag text="禁用深色" disabled mode="dark" custom-class="tag-spacing"></ux-tag>
    </view>
</template>

<style scoped>
.tag-row {
    display: flex;
    flex-direction: row;
    align-items: center;
    flex-wrap: wrap;
}
.tag-spacing {
    margin-right: 10px;
    margin-bottom: 8px;
}
</style>

自定义插槽 ​

通过默认插槽可以自定义标签内容。

查看源码
vue
<template>
    <ux-tag type="success">
        <text style="font-size: 11px; font-weight: bold;">自定义内容</text>
    </ux-tag>
</template>

API ​

Props ​

属性说明类型默认值版本
customStyle自定义根节点样式string | UTSJSONObject--
customClass自定义根节点样式类string''-
type主题类型,primary、success、info、warning、errorThemeType'primary'-
disabled是否禁用boolean | stringfalse-
size尺寸,default/miniTagSize'default'-
shape形状:square-小圆角 3px(默认),round-胶囊,roundLeft/roundRight-单侧胶囊TagShape'square'-
text显示的文本内容string | number''-
color文字颜色string''-
bgColor背景颜色string''-
borderColor边框颜色string''-
closeColor关闭按钮颜色string''-
mode模式,light/dark/plainTagMode'light'-
closeable是否可关闭booleanfalse-
show是否显示booleantrue-

Events ​

事件名说明回调参数版本
click点击标签触发event: UniEvent-
closecloseable为true时,点击标签关闭按钮触发event: UniEvent-

Slots ​

插槽名说明版本
default自定义标签内容(默认显示 text)-

注意事项 ​

  1. disabled 时不派发 click 事件(源码在点击入口直接拦截)。
  2. closeable 的关闭图标点击带 .stop,只触发 close,不会同时触发 click。
  3. close 仅通知,不会自动隐藏标签;需自行把 show 置为 false(示例即如此)。
  4. 关闭图标颜色优先链:close-color > color > dark 模式白色 > type。
  5. mode="plain" 且未传 border-color 时,边框色取文字色 color。
  6. shape 无真直角档(square 为 3px 小圆角);需要直角请用 custom-style 内联 border-radius: 0。
  7. App 端根节点为 display: flex(非 inline-flex),单独使用时标签占满一行,多个标签并排需外层 flex 容器包裹。