跳到主要内容
版本:v6

ion-toast

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,因为它们不应中断用户。

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

控制台
控制台消息将在上方示例中调用 console.log 时显示在此处。

定位

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

相对定位

如果 toast 与导航元素(如头部、底部或 FAB)一起呈现,则默认情况下 toast 可能会与这些元素重叠。这可以使用 positionAnchor 属性来修复,该属性接受元素引用或 ID。toast 将相对于所选元素定位,在使用 position="top" 时出现在其下方,在使用 position="bottom" 时出现在其上方。使用 position="middle" 时,positionAnchor 属性将被忽略。

布局

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

图标

可以在 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 按钮描述

包含文本的按钮在与用户交互时将被屏幕阅读器读取。如果按钮只包含图标,或希望提供现有文本之外的其他描述,可以通过在按钮的 htmlAttributes 属性中传递 aria-label 来为按钮分配标签。

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

提示

虽然这不是完整的列表,但以下是一些使用 toast 时应遵循的指南。

  • 不要要求用户交互才能关闭 toast。例如,在 toast 中包含"关闭"按钮是可以的,但 toast 也应在一段时间后自动关闭。如果你需要用户对通知进行操作,请考虑使用警告框(alert)

  • 对于消息较长的 toast,请考虑调整 duration 属性,让用户有足够的时间阅读 toast 的内容。

接口

ToastButton

interface ToastButton {
text?: string;
icon?: string;
side?: 'start' | 'end';
role?: 'cancel' | string;
cssClass?: string | 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

说明If true, the toast will animate.
属性animated
类型boolean
默认值true

buttons

说明An array of buttons for the toast.
属性undefined
类型(string | ToastButton)[] | undefined
默认值undefined

color

说明The 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.
属性color
类型"danger" | "dark" | "light" | "medium" | "primary" | "secondary" | "success" | "tertiary" | "warning" | string | undefined
默认值undefined

cssClass

说明Additional classes to apply for custom CSS. If multiple classes are provided they should be separated by spaces.
属性css-class
类型string | string[] | undefined
默认值undefined

duration

说明How many milliseconds to wait before hiding the toast. By default, it will show until dismiss() is called.
属性duration
类型number
默认值config.getNumber('toastDuration', 0)

enterAnimation

说明Animation to use when the toast is presented.
属性undefined
类型((baseEl: any, opts?: any) => Animation) | undefined
默认值undefined

header

说明Header to be shown in the toast.
属性header
类型string | undefined
默认值undefined

htmlAttributes

说明Additional attributes to pass to the toast.
属性undefined
类型undefined | { [key: string]: any; }
默认值undefined

icon

说明The name of the icon to display, or the path to a valid SVG file. See ion-icon. https://ionic.io/ionicons
属性icon
类型string | undefined
默认值undefined

isOpen

说明If 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.
属性is-open
类型boolean
默认值false

keyboardClose

说明If true, the keyboard will be automatically dismissed when the overlay is presented.
属性keyboard-close
类型boolean
默认值false

layout

说明Defines 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.
属性layout
类型"baseline" | "stacked"
默认值'baseline'

leaveAnimation

说明Animation to use when the toast is dismissed.
属性undefined
类型((baseEl: any, opts?: any) => Animation) | undefined
默认值undefined

message

说明Message 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.
属性message
类型IonicSafeString | string | undefined
默认值undefined

mode

说明The mode determines which platform styles to use.

这是一个虚拟属性,在初始化时设置一次,之后更改其值不会更新组件。
属性mode
类型"ios" | "md"
默认值undefined

position

说明The starting position of the toast on the screen. Can be tweaked further using the positionAnchor property.
属性position
类型"bottom" | "middle" | "top"
默认值'bottom'

positionAnchor

说明The 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.
属性position-anchor
类型HTMLElement | string | undefined
默认值undefined

swipeGesture

说明If 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.
属性swipe-gesture
类型"vertical" | undefined
默认值undefined

translucent

说明If true, the toast will be translucent. Only applies when the mode is "ios" and the device supports backdrop-filter.
属性translucent
类型boolean
默认值false

trigger

说明An ID corresponding to the trigger element that causes the toast to open when clicked.
属性trigger
类型string | undefined
默认值undefined

事件

Name说明冒泡
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

说明Dismiss the toast overlay after it has been presented.
签名dismiss(data?: any, role?: string) => Promise<boolean>
参数data: 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".

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.

onDidDismiss

说明Returns a promise that resolves when the toast did dismiss.
签名onDidDismiss<T = any>() => Promise<OverlayEventDetail<T>>

onWillDismiss

说明Returns a promise that resolves when the toast will dismiss.
签名onWillDismiss<T = any>() => Promise<OverlayEventDetail<T>>

present

说明Present the toast overlay after it has been created.
签名present() => Promise<void>

CSS 阴影部分

Name说明
buttonAny button element that is displayed inside of the toast.
button cancelAny button element with role "cancel" that is displayed inside of the toast.
containerThe element that wraps all child elements.
headerThe header text of the toast.
iconThe icon that appears next to the toast content.
messageThe body text of the toast.

CSS 自定义属性

Name说明
--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

插槽

该组件没有可用的插槽。