Skip to content

Alert 警告提示 ​

以横幅形式展示需要用户注意的成功、信息、警告或错误状态。

何时使用 ​

  • 在页面内容中展示持续可见的状态、风险或操作结果。
  • 需要用户主动关闭,或需要附加操作(如撤销、重试)时使用。
  • 只需短暂反馈时用 Message;需要确认操作时用 Popconfirm。

在线体验 ​

Alert 警告提示 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

go
import "github.com/tangoui/tango/ui"

func alertPage(c *ui.Context) {
    ui.Alert(c, "info", "有新版本可用").Show()
}

status 支持 success、info、warning、error。链式设置完成后调用 Show() 构建组件。

常见用法 ​

辅助说明与关闭 ​

go
func alertDescription(c *ui.Context) {
    ui.Alert(c, "warning", "请检查配置").
        Description("保存前仍有两个必填项未完成。").Closable().Show()
}

Closable() 显示关闭按钮。调用 Show() 后,用 Closed() 读取本帧的关闭动作并更新业务状态。

深色效果与居中 ​

go
func alertEffects(c *ui.Context) {
    ui.Alert(c, "success", "发布成功").Effect(ui.AlertEffectDark).Center().Show()
    ui.Alert(c, "error", "发布失败").NoIcon().Show()
}

Effect(ui.AlertEffectDark) 使用状态色实底;Center() 居中文本和图标;NoIcon() 隐藏状态图标。

附加操作 ​

go
func alertAction(c *ui.Context) {
    ui.Alert(c, "info", "草稿已保存").Action(func() {
        ui.Button(c, "撤销").Small()
    }).Show()
}

Action 的回调构建右侧操作区,适合放置小型按钮;不要在其中塞入复杂表单。

go
func alertBanner(c *ui.Context) {
    ui.Alert(c, "warning", "系统将在今晚维护").Banner().Show()
}

Banner() 适合放在页面或窗口顶部,默认会显示状态图标。普通提示的图标默认隐藏,可用 ShowIcon(true) 打开。

主题与样式 ​

Alert 的状态色、圆角、间距和图标由当前主题控制。ConfigProvider.Defaults("Alert", ...) 支持 showIcon、closable、banner 三个布尔值和 variant 字符串。Ant Design 的 Variant("outlined") 默认带状态色边框,Variant("filled") 去掉边框。Effect(ui.AlertEffectDark) 对应 Element Plus 的深色实底,shadcn 使用带边框的 Alert 卡片。

注意事项 ​

即时模式下,把是否显示、业务文案等状态放在页面函数外或 ui.State 中,避免每帧重置。关闭后应由业务状态决定是否再次构建。可关闭提示必须提供明确文案;键盘用户可以聚焦关闭按钮并用 Enter 或空格操作。

示例代码 ​

以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。

Alert 警告提示

gallery 演示 Alert 的 Render 函数,源码 page_feedback.go#L10。

加载中…

Alert 图标、变体与关闭

gallery 演示 AlertAlign 的 Render 函数,源码 page_b3l.go#L22。

加载中…

Alert 操作区

gallery 演示 AlertMore 的具名 Render 函数,源码 page_p2c_display.go#L122。

加载中…

API ​

签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。

入口函数 ​

ui.Alert ​

go
func Alert(c *Context, status, title string) *AlertW

Alert adds a banner; call Show after options. status: success, info, warning, error. title is antd's title (v6; message before).

As in antd and Element Plus the icon is off unless ShowIcon(true) (or Banner, which shows it by default).

ConfigProvider.Defaults("Alert", ...) keys: "showIcon", "closable", "banner" (bool), "variant" (string: "outlined" | "filled").

Under shadcn/ui it is the bordered Alert card with an outlined (lucide-style) icon; "error" is the destructive variant.

返回 *AlertW,可链式调用的方法见 AlertW 方法。

AlertW 方法 ​

ui.AlertW:AlertW is an inline status banner.

方法说明签名
ActionAction builds an operation area at the right of the text (Ant action), e.g. a small button.Action(fn func()) *AlertW
BannerBanner styles the alert as a page-top banner: no border or radius, and the icon on unless ShowIcon(false) (antd banner).Banner() *AlertW
CenterCenter centers the icon and text horizontally (Element center); the close button stays at the end.Center() *AlertW
ClosableClosable adds the close button.Closable() *AlertW
CloseDisabledCloseDisabled disables the close button (antd closable.disabled); ConfigProvider.ComponentDisabled disables it too.CloseDisabled(on bool) *AlertW
CloseIconCloseIcon replaces the close icon and makes the alert closable (antd closable.closeIcon).CloseIcon(ic *icon.Icon) *AlertW
CloseTextCloseText shows text instead of the close icon and makes the alert closable (EP close-text).CloseText(s string) *AlertW
ClosedClosed reports a click on the close button.Closed() bool
DescriptionDescription(d string) *AlertW
EffectEffect selects the fill: AlertEffectLight (default) or AlertEffectDark, the solid status color with contrasting text (Element effect="dark").Effect(e AlertEffect) *AlertW
IconIcon replaces the status icon (antd icon) and shows it.Icon(ic *icon.Icon) *AlertW
NoIconNoIcon hides the status icon (same as ShowIcon(false)).NoIcon() *AlertW
ShowShow builds the alert.Show() *AlertW
ShowIconShowIcon shows or hides the status icon (antd showIcon, EP show-icon; default off, on for a Banner).ShowIcon(on bool) *AlertW
VariantVariant selects "outlined" (default, status border) or "filled" (no border) (antd 6.4 variant).Variant(v string) *AlertW

相关链接 ​

以 MIT 许可发布