# new NgxToastComponent(options)
This class provides a wrapper around Ionic's toast controller to display various types of toast notifications (error, warning, success, information). It manages toast creation, presentation, and dismissal, ensuring only one toast is displayed at a time. The component uses a singleton pattern to maintain a single instance throughout the application.
Toast notification component for Angular applications
Parameters:
Name | Type | Description |
---|---|---|
options |
ToastOptions
|
Configuration options for the toast notifications |
Example
```typescript
// Get the toast component instance
const toast = getNgxToastComponent();
// Display different types of notifications
toast.success('Operation completed successfully');
toast.error('An error occurred');
toast.warn('Warning: This action cannot be undone');
toast.inform('The process is starting');
```
Classes
- NgxToastComponent
Initializes the toast component with the provided options, merging them with the default options.
- NgxToastComponent
Initializes the toast component with the provided options, merging them with the default options.
Members
# options
Stores the toast configuration options, combining default options with any custom options provided during initialization.
Configuration options for toast notifications
Methods
# async error(message) → {Promise.<ToastRole>}
Shows a toast notification with error styling (danger color). This method is used to display error messages to the user. It waits for the toast to be dismissed and returns the dismissal role.
Displays an error toast notification
Parameters:
Name | Type | Description |
---|---|---|
message |
string
|
The error message to display |
A promise that resolves to the toast dismissal role
Promise.<ToastRole>
# async handleDidDismiss(toast) → {Promise.<OverlayEventDetail>}
Internal method that waits for a toast to be dismissed and cleans up the component reference. It returns the dismissal data, which includes the role that indicates how the toast was dismissed (e.g., timeout, user interaction).
Handles toast dismissal events
Parameters:
Name | Type | Description |
---|---|---|
toast |
HTMLIonToastElement
|
The toast element to handle dismissal for |
A promise that resolves to the dismissal data
Promise.<OverlayEventDetail>
# async inform(message) → {Promise.<void>}
Shows a toast notification with default styling (no specific color). This method is used to display general informational messages to the user. Unlike error and warning methods, it doesn't wait for dismissal or return a role.
Displays an informational toast notification
Parameters:
Name | Type | Description |
---|---|---|
message |
string
|
The informational message to display |
A promise that resolves when the toast is displayed
Promise.<void>
# async show(options) → {Promise.<HTMLIonToastElement>}
Internal method that handles the creation and presentation of toast notifications. It ensures only one toast is displayed at a time by dismissing any existing toast before creating a new one. It also adds a small delay after dismissal to prevent visual glitches.
Creates and displays a toast notification
Parameters:
Name | Type | Description |
---|---|---|
options |
ToastOptions
|
Configuration options for the toast |
A promise that resolves to the created toast element
Promise.<HTMLIonToastElement>
# async success(message) → {Promise.<void>}
Shows a toast notification with success styling (green color). This method is used to display success messages to the user, typically after an operation has completed successfully. Like the inform method, it doesn't wait for dismissal or return a role.
Displays a success toast notification
Parameters:
Name | Type | Description |
---|---|---|
message |
string
|
The success message to display |
A promise that resolves when the toast is displayed
Promise.<void>
# async warn(message) → {Promise.<ToastRole>}
Shows a toast notification with warning styling (yellow/amber color). This method is used to display warning messages to the user, typically for actions that require caution. Like the error method, it waits for the toast to be dismissed and returns the dismissal role.
Displays a warning toast notification
Parameters:
Name | Type | Description |
---|---|---|
message |
string
|
The warning message to display |
A promise that resolves to the toast dismissal role
Promise.<ToastRole>