addFlashMessage(flashMessageOptions: TFlashMessageOptions, containerName?: string): string¶
The addFlashMessage
method adds a flash message to the page. It requires an existing flash-messenger webcomponent to be present on the page.
Input parameters¶
flashMessage¶
flashMessage
is a mandatory parameter of the TFlashMessageOptions type.
containerName (optional)¶
string
is a mandatory parameter that tell the api which flash-messenger should it refer to. If not specified the message will be added to the global flash messenger.
Returned value¶
addFlashMessage
return value is of type string
and contains an ID of the added flash message.
Event Bus events¶
This API method dispatches the following events with the Event Bus:
- FlashMessengerApi.addFlashMessage - when displaying a flash message
Example¶
Making a Flash Messages API call to add a flash message to a Flash Messenger webcomponent with the name="my-flash-messenger"
attribute if it exists.
Example¶
Making a Flash Messages API call to add a flash message to a global Flash Messenger webcomponent.
useStorefront(async (storefront) => {
const flashMessengerApi = storefront.getApiSync('FlashMessengerApi');
flashMessengerApi.addFlashMessage({
content: `You got a new message`,
type: 'info',
isCloseable: true,
timeout: 10000
});
});