快餐栏 Snackbar

snack-bar 的 API

Angular Material snack-bar API 参考文档

import {MatSnackBarModule} from '@angular/material/snack-bar';

一个服务,用来派发 Material Design 快餐栏的消息。

Service to dispatch Material Design snack bar messages.

属性
名称 描述

handsetCssClass: 'mat-snack-bar-handset'

适用于手机模式的 CSS 类。

The CSS class to applie for handset mode.

simpleSnackBarComponent: Type<TextOnlySnackBar>

该组件应该被渲染成快餐栏的简单组件。

The component that should be rendered as the snack bar's simple component.

snackBarContainerComponent: Type<_SnackBarContainer>

附加在所提供的模板或组件上的容器组件。

The container component that attaches the provided template or component.

方法
dismiss

关闭目前可见的快餐栏。

Dismisses the currently-visible snack bar.

open

带有信息和可选操作的快餐栏。

Opens a snackbar with a message and an optional action.

参数

Parameters

message

string

要显示在快餐栏里的消息。

The message to show in the snackbar.

action

string = ''

快餐栏的动作标签。

The label for the snackbar action.

config?

MatSnackBarConfig<any>

快餐栏的其它配置选项。

Additional configuration options for the snackbar.

返回值

Returns

MatSnackBarRef<TextOnlySnackBar>
openFromComponent

为内容创建并派发带有自定义组件的快餐栏,删除任何目前打开着的快餐栏。

Creates and dispatches a snack bar with a custom component for the content, removing any currently opened snack bars.

参数

Parameters

component

ComponentType<T>

要实例化的组件。

Component to be instantiated.

config?

MatSnackBarConfig<any>

快餐栏的额外配置。

Extra configuration for the snack bar.

返回值

Returns

MatSnackBarRef<T>
openFromTemplate

使用该内容的自定义模板创建并派发一个快餐栏,删除任何目前打开着的快餐栏。

Creates and dispatches a snack bar with a custom template for the content, removing any currently opened snack bars.

参数

Parameters

template

TemplateRef<any>

要实例化的模板

Template to be instantiated.

config?

MatSnackBarConfig<any>

快餐栏的额外配置。

Extra configuration for the snack bar.

返回值

Returns

MatSnackBarRef<EmbeddedViewRef<any>>

用来打开默认快餐栏的组件,匹配 Material 规范。这应该只在快餐栏服务的内部使用。

A component used to open as the default snack bar, matching material spec. This should only be used internally by the snack bar service.

选择器: simple-snack-bar
属性
名称 描述

data: { message: string; action: string; }

那些注入快餐栏的数据。

Data that was injected into the snack bar.

hasAction: boolean

是否要显示动作按钮。

If the action button should be shown.

snackBarRef: MatSnackBarRef<SimpleSnackBar>

方法
action

执行快餐店里的动作。

Performs the action on the snack bar.

打开快餐栏时使用的配置。

Configuration used when opening a snack-bar.

属性
名称 描述

announcementMessage: string

将由 LiveAnnouncer 朗读的消息。当没有自定义组件或模板的情况下打开快餐栏时,公告信息会默认为指定的消息。

Message to be announced by the LiveAnnouncer. When opening a snackbar without a custom component or template, the announcement message will default to the specified message.

data: D | null

要注入到子组件中的数据。

Data being injected into the child component.

direction: Direction

快餐栏的文字布局方向。

Text layout direction for the snack bar.

duration: number

在自动关闭快餐栏之前要等待的毫秒数。

The length of time in milliseconds to wait before automatically dismissing the snack bar.

horizontalPosition: MatSnackBarHorizontalPosition

放置快餐栏的水平位置。

The horizontal position to place the snack bar.

panelClass: string | string[]

要添加到快餐栏容器中的额外 CSS 类。

Extra CSS classes to be added to the snack bar container.

politeness: AriaLivePoliteness

MatAriaLiveAnnouncer 公告的文雅程度。

The politeness level for the MatAriaLiveAnnouncer announcement.

verticalPosition: MatSnackBarVerticalPosition

放置快餐栏的垂直位置。

The vertical position to place the snack bar.

viewContainerRef: ViewContainerRef

视图容器的引用,可以在依赖注入时作为快餐栏的父组件。注意:这不会影响快餐栏在 DOM 中的插入位置。

The view container that serves as the parent for the snackbar for the purposes of dependency injection. Note: this does not affect where the snackbar is inserted in the DOM.

到快餐栏服务派发出的快餐栏的引用。

Reference to a snack bar dispatched from the snack bar service.

属性
名称 描述

instance: T

该组件的实例构成了快餐栏的内容。

The instance of the component making up the content of the snack bar.

方法
afterDismissed

获取一个当快餐栏结束时会得到通知的可观察对象。

Gets an observable that is notified when the snack bar is finished closing.

返回值

Returns

Observable<MatSnackBarDismiss>
afterOpened

获取一个可观察对象,当快餐栏已经打开并出现时,会通知它。

Gets an observable that is notified when the snack bar has opened and appeared.

返回值

Returns

Observable<void>
dismiss

关闭快餐栏。

Dismisses the snack bar.

dismissWithAction

标记某个快餐栏动作被点击过。

Marks the snackbar action clicked.

onAction

获取一个会在调用小吃条动作时得到通知的可观察对象。

Gets an observable that is notified when the snack bar action is called.

返回值

Returns

Observable<void>
已弃用
closeWithAction

标记某个快餐栏动作被点击过。

Marks the snackbar action clicked.

快餐栏被关闭时发生的事件。

Event that is emitted when a snack bar is dismissed.

属性
名称 描述

dismissedByAction: boolean

快餐栏是否被动作按钮关闭了。

Whether the snack bar was dismissed using the action button.

简单快餐栏组件的接口,它包含一条消息和一个动作。

Interface for a simple snack bar component that has a message and a single action.

属性
名称 描述

action: () => void

data: { message: string; action: string; }

hasAction: boolean

snackBarRef: MatSnackBarRef<TextOnlySnackBar>

MatSnackBarConfig 中 horizontalPosition 的可选值。

Possible values for horizontalPosition on MatSnackBarConfig.

type MatSnackBarHorizontalPosition = 'start' | 'center' | 'end' | 'left' | 'right';

MatSnackBarConfig 中 verticalPosition 的可选值。

Possible values for verticalPosition on MatSnackBarConfig.

type MatSnackBarVerticalPosition = 'top' | 'bottom';

这个注入令牌可以用来指定快餐栏的默认配置。

Injection token that can be used to specify default snack bar.

const MAT_SNACK_BAR_DEFAULT_OPTIONS: InjectionToken<MatSnackBarConfig<any>>;

这个注入令牌可以用来访问传入快餐栏的数据。

Injection token that can be used to access the data that was passed in to a snack bar.

const MAT_SNACK_BAR_DATA: InjectionToken<any>;

Angular Material snack-bar-testing API 参考文档

import {MatSnackBarHarness} from '@angular/material/snack-bar/testing';

在测试中用来与标准 mat-snack-bar 进行交互的测试工具。

Harness for interacting with a standard mat-snack-bar in tests.

方法
异步
dismissWithAction

Dismisses the snack-bar by clicking the action button. Method cannot be used for snack-bar's without action or with custom content.

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
getActionDescription

Gets the description of the snack-bar. Method cannot be used for snack-bar's without action or with custom content.

返回值

Returns

Promise<string>
异步
getAllChildLoaders

参数

Parameters

selector

S

返回值

Returns

Promise<HarnessLoader[]>
异步
getAllHarnesses

参数

Parameters

query

HarnessQuery<T>

返回值

Returns

Promise<T[]>
异步
getAriaLive

Gets the aria-live of the snack-bar's live region. The aria-live of a snack-bar is determined based on the ARIA politeness specified in the snack-bar config.

返回值

Returns

Promise<AriaLivePoliteness>
异步
getChildLoader

参数

Parameters

selector

S

返回值

Returns

Promise<HarnessLoader>
异步
getHarness

参数

Parameters

query

HarnessQuery<T>

返回值

Returns

Promise<T>
异步
getMessage

Gets the message of the snack-bar. Method cannot be used for snack-bar's with custom content.

返回值

Returns

Promise<string>
异步
hasAction

Whether the snack-bar has an action. Method cannot be used for snack-bar's with custom content.

返回值

Returns

Promise<boolean>
异步
host

获取一个代表该组件宿主元素的 TestElementPromise

Gets a Promise for the TestElement representing the host element of the component.

返回值

Returns

Promise<TestElement>
异步
isDismissed

Gets whether the snack-bar has been dismissed.

返回值

Returns

Promise<boolean>
已弃用
异步
getRole

Gets the role of the snack-bar. The role of a snack-bar is determined based on the ARIA politeness specified in the snack-bar config.

返回值

Returns

Promise<'alert' | 'status' | null>

A set of criteria that can be used to filter a list of MatSnackBarHarness instances.