跳到主要内容
版本:v8

可关闭的应用通知提示组件

shadow

Toast 是现代应用中常见的轻量级通知。它可用于提供操作反馈或显示系统消息。Toast 会显示在应用内容的上方,并且可以由应用关闭以恢复用户与应用之间的交互。

内联 Toast(推荐)

通过在模板中直接编写 ion-toast 组件,即可使用它。这减少了为呈现 Toast 而需要连接的处理程序数量。

使用 isOpen

ion-toast 上的 isOpen 属性允许开发者通过其应用状态来控制 Toast 的呈现状态。这意味着当 isOpen 设置为 true 时,Toast 将被呈现;当 isOpen 设置为 false 时,Toast 将被关闭。

isOpen 使用单向数据绑定,这意味着当 Toast 被关闭时,它不会自动设置为 false。开发者应监听 ionToastDidDismissdidDismiss 事件,并将 isOpen 设置为 false。这样做是为了防止 ion-toast 的内部实现与应用状态紧密耦合。使用单向数据绑定时,Toast 只需要关注响应式变量提供的布尔值。而使用双向数据绑定时,Toast 需要同时关注布尔值和响应式变量本身的存在。这可能导致非确定性行为,并使应用更难调试。

控制器 Toast

关闭

Toast 旨在作为轻量级通知,不应打断用户。因此,不应要求用户交互来关闭 Toast。

可以通过在 Toast 选项的 duration 中传递显示毫秒数,使 Toast 在特定时间后自动关闭。如果添加了角色为 "cancel" 的按钮,则该按钮将关闭 Toast。要在创建后关闭 Toast,请调用实例上的 dismiss() 方法。

由于 Toast 不应打断用户,因此按下硬件返回键不会关闭 Toast。

以下示例演示了如何使用 buttons 属性添加一个在点击时自动关闭 Toast 的按钮,以及如何获取关闭事件的 role

Console
Console messages will appear here when logged from the example above.

定位

Toast 可以定位在视口的顶部、底部或中间。可以在创建时传入位置。可能的值为 topbottommiddle。如果未指定位置,Toast 将显示在视口的底部。

相对定位

如果 Toast 与导航元素(如标题、页脚或 FAB)一同呈现,默认情况下 Toast 可能会覆盖这些元素。可以使用 positionAnchor 属性来解决此问题,该属性接受元素引用或 ID。Toast 将相对于所选元素进行定位:当使用 position="top" 时,Toast 会出现在该元素下方;当使用 position="bottom" 时,Toast 会出现在该元素上方。使用 position="middle" 时,positionAnchor 属性将被忽略。

滑动关闭

通过使用 swipeGesture 属性,可以滑动关闭 Toast。此功能具有位置感知能力,意味着用户需要滑动的方向会根据 position 属性的值而变化。此外,用户需要滑动的距离可能会受到 positionAnchor 属性的影响。

布局

Toast 内的按钮容器可以使用 layout 属性显示在与消息同一行,或堆叠在不同行上。堆叠布局应用于具有长文本值的按钮。此外,堆叠 Toast 布局中的按钮可以使用 side 值为 startend,但不能同时使用两者。

图标

可以在 Toast 内容旁添加图标。一般来说,Toast 中的图标应用于增加额外的样式或上下文,而不是吸引用户注意力或提升 Toast 的优先级。如果您希望向用户传达更高优先级的消息或确保得到响应,建议改用 Alert

主题

无障碍访问

焦点管理

Toast 旨在作为轻量级通知,不应打断用户。因此,不应要求用户交互来关闭 Toast。因此,当呈现 Toast 时,焦点不会自动移动到 Toast。

屏幕阅读器

Toast 设置了 aria 属性,以便屏幕阅读器可以 无障碍访问,但如果这些属性描述不够充分或与 Toast 在应用中的使用方式不符,可以覆盖这些属性。

角色

ion-toast 在内层的 .toast-content 元素上设置了 role="status"aria-live="polite"。这导致屏幕阅读器仅会播报 Toast 消息和标题。当 Toast 呈现时,按钮和图标不会被播报。

aria-live 会导致屏幕阅读器在 Toast 内容更新时播报内容。但是,由于该属性设置为 'polite',屏幕阅读器不应打断当前任务。

由于 Toast 旨在作为轻量级通知,aria-live 绝不应设置为 "assertive"。如果开发者需要用重要消息打断用户,建议使用 alert

Toast 按钮描述

包含文本的按钮在用户与之交互时会被屏幕阅读器播报。如果按钮仅包含图标,或者需要除现有文本以外的描述,可以通过将 aria-label 传递给按钮上的 htmlAttributes 属性来为按钮分配标签。

const toast = await this.toastController.create({
header: 'Header',
buttons: [
{
icon: 'close',
htmlAttributes: {
'aria-label': 'close',
},
},
],
});

使用建议

虽然这不是一个完整的列表,但以下是在使用 Toast 时应遵循的一些准则。

  • 不要要求用户交互来关闭 Toast。例如,Toast 中有“关闭”按钮是可以的,但 Toast 也应该在超时后自动关闭。如果通知需要用户交互,请考虑改用 alert

  • 对于包含长消息的 Toast,请考虑调整 duration 属性,以便为用户提供足够的时间来阅读 Toast 内容。

  • 如果向 Toast 添加按钮,请始终为每个按钮关联的操作提供替代完成方式。这确保了即使用户在阅读之前 Toast 就关闭了,他们仍然可以完成 Toast 中显示的操作。

  • 避免在另一个覆盖层(如 modal)内显示带有按钮的 Toast。Modal 和其他覆盖层实现了 焦点陷阱,会阻止屏幕阅读器将焦点移动到 Toast 以完成操作。这可能会让用户感到困惑,因为屏幕阅读器仍然会播报 Toast。即使为每个按钮关联的操作实现了替代完成方式,也是如此。请考虑在焦点陷阱的 Modal 内创建一个 实时区域,而不是使用 Toast。

接口

ToastButton

interface ToastButton {
text?: string;
icon?: string;
side?: 'start' | 'end';
role?: 'cancel' | string;
htmlAttributes?: { [key: string]: any };
handler?: () => boolean | void | Promise<boolean | void>;
}

ToastOptions

interface ToastOptions {
header?: string;
message?: string | IonicSafeString;
cssClass?: string | string[];
duration?: number;
buttons?: (ToastButton | string)[];
position?: 'top' | 'bottom' | 'middle';
translucent?: boolean;
animated?: boolean;
icon?: string;
htmlAttributes?: { [key: string]: any };

color?: Color;
mode?: Mode;
keyboardClose?: boolean;
id?: string;

enterAnimation?: AnimationBuilder;
leaveAnimation?: AnimationBuilder;
}

属性

animated

DescriptionIf true, the toast will animate.
Attributeanimated
Typeboolean
Defaulttrue

buttons

DescriptionAn array of buttons for the toast.
Attributeundefined
Type(string | ToastButton)[] | undefined
Defaultundefined

color

DescriptionThe color to use from your application's color palette. Default options are: "primary", "secondary", "tertiary", "success", "warning", "danger", "light", "medium", and "dark". For more information on colors, see theming.
Attributecolor
Type"danger" | "dark" | "light" | "medium" | "primary" | "secondary" | "success" | "tertiary" | "warning" | string | undefined
Defaultundefined

cssClass

DescriptionAdditional classes to apply for custom CSS. If multiple classes are provided they should be separated by spaces.
Attributecss-class
Typestring | string[] | undefined
Defaultundefined

duration

DescriptionHow many milliseconds to wait before hiding the toast. By default, it will show until dismiss() is called.
Attributeduration
Typenumber
Defaultconfig.getNumber('toastDuration', 0)

enterAnimation

DescriptionAnimation to use when the toast is presented.
Attributeundefined
Type((baseEl: any, opts?: any) => Animation) | undefined
Defaultundefined
DescriptionHeader to be shown in the toast.
Attributeheader
Typestring | undefined
Defaultundefined

htmlAttributes

DescriptionAdditional attributes to pass to the toast.
Attributeundefined
Typeundefined | { [key: string]: any; }
Defaultundefined

icon

DescriptionThe name of the icon to display, or the path to a valid SVG file. See ion-icon. https://ionic.io/ionicons
Attributeicon
Typestring | undefined
Defaultundefined

isOpen

DescriptionIf true, the toast will open. If false, the toast will close. Use this if you need finer grained control over presentation, otherwise just use the toastController or the trigger property. Note: isOpen will not automatically be set back to false when the toast dismisses. You will need to do that in your code.
Attributeis-open
Typeboolean
Defaultfalse

keyboardClose

DescriptionIf true, the keyboard will be automatically dismissed when the overlay is presented.
Attributekeyboard-close
Typeboolean
Defaultfalse

layout

DescriptionDefines how the message and buttons are laid out in the toast. 'baseline': The message and the buttons will appear on the same line. Message text may wrap within the message container. 'stacked': The buttons containers and message will stack on top of each other. Use this if you have long text in your buttons.
Attributelayout
Type"baseline" | "stacked"
Default'baseline'

leaveAnimation

DescriptionAnimation to use when the toast is dismissed.
Attributeundefined
Type((baseEl: any, opts?: any) => Animation) | undefined
Defaultundefined

message

DescriptionMessage to be shown in the toast. This property accepts custom HTML as a string. Content is parsed as plaintext by default. innerHTMLTemplatesEnabled must be set to true in the Ionic config before custom HTML can be used.
Attributemessage
TypeIonicSafeString | string | undefined
Defaultundefined

mode

DescriptionThe mode determines which platform styles to use.

This is a virtual property that is set once during initialization and will not update if you change its value after the initial render.
Attributemode
Type"ios" | "md"
Defaultundefined

position

DescriptionThe starting position of the toast on the screen. Can be tweaked further using the positionAnchor property.
Attributeposition
Type"bottom" | "middle" | "top"
Default'bottom'

positionAnchor

DescriptionThe element to anchor the toast's position to. Can be set as a direct reference or the ID of the element. With position="bottom", the toast will sit above the chosen element. With position="top", the toast will sit below the chosen element. With position="middle", the value of positionAnchor is ignored.
Attributeposition-anchor
TypeHTMLElement | string | undefined
Defaultundefined

swipeGesture

DescriptionIf set to 'vertical', the Toast can be dismissed with a swipe gesture. The swipe direction is determined by the value of the position property: top: The Toast can be swiped up to dismiss. bottom: The Toast can be swiped down to dismiss. middle: The Toast can be swiped up or down to dismiss.
Attributeswipe-gesture
Type"vertical" | undefined
Defaultundefined

translucent

DescriptionIf true, the toast will be translucent. Only applies when the mode is "ios" and the device supports backdrop-filter.
Attributetranslucent
Typeboolean
Defaultfalse

trigger

DescriptionAn ID corresponding to the trigger element that causes the toast to open when clicked.
Attributetrigger
Typestring | undefined
Defaultundefined

事件

NameDescriptionBubbles
didDismissEmitted after the toast has dismissed. Shorthand for ionToastDidDismiss.true
didPresentEmitted after the toast has presented. Shorthand for ionToastWillDismiss.true
ionToastDidDismissEmitted after the toast has dismissed.true
ionToastDidPresentEmitted after the toast has presented.true
ionToastWillDismissEmitted before the toast has dismissed.true
ionToastWillPresentEmitted before the toast has presented.true
willDismissEmitted before the toast has dismissed. Shorthand for ionToastWillDismiss.true
willPresentEmitted before the toast has presented. Shorthand for ionToastWillPresent.true

方法

dismiss

DescriptionDismiss the toast overlay after it has been presented. This is a no-op if the overlay has not been presented yet. If you want to remove an overlay from the DOM that was never presented, use the remove method.
Signaturedismiss(data?: any, role?: string) => Promise<boolean>
Parametersdata: Any data to emit in the dismiss events.
role: The role of the element that is dismissing the toast. This can be useful in a button handler for determining which button was clicked to dismiss the toast. Some examples include: "cancel", "destructive", "selected", and "backdrop".

onDidDismiss

DescriptionReturns a promise that resolves when the toast did dismiss.
SignatureonDidDismiss<T = any>() => Promise<OverlayEventDetail<T>>

onWillDismiss

DescriptionReturns a promise that resolves when the toast will dismiss.
SignatureonWillDismiss<T = any>() => Promise<OverlayEventDetail<T>>

present

DescriptionPresent the toast overlay after it has been created.
Signaturepresent() => Promise<void>

CSS 影子部件

NameDescription
buttonAny button element that is displayed inside of the toast.
button cancelAny button element with role "cancel" that is displayed inside of the toast.
containerGroups the icon, content, and buttons.
contentThe live region that contains the header and message.
headerThe header text of the toast.
iconThe icon that appears next to the toast content.
messageThe body text of the toast.
wrapperThe outer wrapper for the toast overlay.

CSS 自定义属性

NameDescription
--backgroundBackground of the toast
--border-colorBorder color of the toast
--border-radiusBorder radius of the toast
--border-styleBorder style of the toast
--border-widthBorder width of the toast
--box-shadowBox shadow of the toast
--button-colorColor of the button text
--colorColor of the toast text
--endPosition from the right if direction is left-to-right, and from the left if direction is right-to-left
--heightHeight of the toast
--max-heightMaximum height of the toast
--max-widthMaximum width of the toast
--min-heightMinimum height of the toast
--min-widthMinimum width of the toast
--startPosition from the left if direction is left-to-right, and from the right if direction is right-to-left
--white-spaceWhite space of the toast message
--widthWidth of the toast

插槽

No slots available for this component.