125 lines
4.0 KiB
Markdown
125 lines
4.0 KiB
Markdown
---
|
|
title: ErrorMessage 错误消息
|
|
description: 用于展示错误的底层错误消息组件
|
|
component: error-message
|
|
group: 组件
|
|
---
|
|
|
|
## 用法
|
|
|
|
```vue
|
|
<script setup lang="ts">
|
|
import { ErrorMessage } from "@heroui-vue/components";
|
|
</script>
|
|
|
|
<template>
|
|
<ErrorMessage>请至少选择一个分类</ErrorMessage>
|
|
</template>
|
|
```
|
|
|
|
<ComponentPreview name="error-message-basic" />
|
|
|
|
`ErrorMessage` 是基于固定基线 `Text` 组件 `errorMessage` slot 的底层组件,用于在 **非表单组件**(如 `TagGroup`、`Calendar` 及其他集合类组件)中展示错误消息。
|
|
|
|
## 组件结构
|
|
|
|
```vue
|
|
<script setup lang="ts">
|
|
import {
|
|
Description,
|
|
ErrorMessage,
|
|
Label,
|
|
Tag,
|
|
TagGroup,
|
|
TagGroupList,
|
|
} from "@heroui-vue/components";
|
|
</script>
|
|
|
|
<template>
|
|
<TagGroup>
|
|
<Label>分类</Label>
|
|
<TagGroupList>
|
|
<Tag id="news">资讯</Tag>
|
|
</TagGroupList>
|
|
<Description>说明</Description>
|
|
<ErrorMessage>请至少选择一个分类</ErrorMessage>
|
|
</TagGroup>
|
|
</template>
|
|
```
|
|
|
|
## 何时使用
|
|
|
|
`ErrorMessage` **不绑定表单**,是用于非表单上下文的通用错误展示组件。
|
|
|
|
- **推荐用于** 非表单组件(如 `TagGroup`、`Calendar`、集合类组件)
|
|
- **表单字段** 建议使用 [FieldError](./field-error),它提供表单专用校验特性与自动错误处理,遵循标准化表单校验模式
|
|
|
|
## ErrorMessage 与 FieldError
|
|
|
|
| 组件 | 使用场景 | 表单集成 | 示例组件 |
|
|
| -------------- | ---------- | -------- | ------------------------------------ |
|
|
| `ErrorMessage` | 非表单组件 | 否 | `TagGroup`、`Calendar` |
|
|
| `FieldError` | 表单字段 | 是 | `TextField`、`NumberField`、`Select` |
|
|
|
|
表单校验建议使用 `FieldError`,它遵循标准化表单校验模式并提供表单专用特性。
|
|
|
|
## 自定义样式
|
|
|
|
### Tailwind CSS
|
|
|
|
<ComponentPreview name="error-message-custom-styles" />
|
|
|
|
### 全局 CSS
|
|
|
|
可使用 `@layer components` 指令自定义 ErrorMessage 组件类。[了解更多](https://tailwindcss.com/docs/adding-custom-styles#adding-component-classes)。
|
|
|
|
```css
|
|
@layer components {
|
|
.hv-error-message {
|
|
@apply text-sm font-medium text-danger;
|
|
}
|
|
}
|
|
```
|
|
|
|
## 样式参考
|
|
|
|
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 `hv-`。
|
|
|
|
### CSS 类
|
|
|
|
ErrorMessage 组件使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/error-message.css)):
|
|
|
|
#### 基础类
|
|
|
|
- `.hv-error-message` - 基础错误消息样式,danger 色与文本换行
|
|
|
|
#### 插槽类
|
|
|
|
- `[slot="errorMessage"]` - 与固定基线一致的 DOM 标记,可用作 CSS 选择器
|
|
- `[data-slot="error-message"]` - 组件标记
|
|
|
|
## API 参考
|
|
|
|
### ErrorMessage
|
|
|
|
| Prop | 类型 | 默认值 | 描述 |
|
|
| -------- | --------------------------- | ------ | ----------------------------- |
|
|
| `id` | `string` | 自动 | 元素 id;位于字段内时参与描述 |
|
|
| 默认插槽 | `Component` | - | 错误消息内容 |
|
|
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
|
|
|
|
## 说明
|
|
|
|
`ErrorMessage` 渲染为 `<p>`,带 `slot="errorMessage"` 与 `data-slot="error-message"` 标记;在 Vue 中 `slot` 是普通 DOM 属性(CSS 钩子),不是插槽分配——直接把 `ErrorMessage` 放进父组件的默认插槽即可。显示与隐藏由调用方根据自身状态控制,组件本身不做校验。
|
|
|
|
## 无障碍
|
|
|
|
- 使用语义化 HTML 展示错误文本
|
|
- 放在字段或标签组内时,其 `id` 会进入所属容器的 `aria-describedby`
|
|
- 错误状态使用主题的 danger 色,保持足够对比度
|
|
- 上游经 `Text` 的 `errorMessage` slot 提供节点;本库保留同样的 DOM 标记,但不改写 `TagGroup` 的 `aria-invalid`
|
|
|
|
## 相关组件
|
|
|
|
[FieldError 字段错误](./field-error) · [Description 描述](./description) · [TagGroup 标签组](./tag-group)
|