my.showToast displays a toast notification that automatically disappears after a specified duration. Toasts are non-blocking notifications used for brief feedback messages. my.hideToast manually dismisses the toast before its duration expires.
Show the toast dialog, which disappears with the specified duration.
my.showToast({
type: 'success',
content: 'Success',
duration: 3000,
success: () => {
my.alert({
title: 'toast is missing',
});
},
});
| Property | Type | Required | Description |
|---|---|---|---|
content | String | No | Text content. |
type | String | No | Toast type, showing the related icon, none by default. Supporting success/fail/exception/none. If it is exception, content is mandatory. |
duration | Number | No | Displaying duration, in ms, 2000 by default. |
success | Function | No | Callback function upon call success. |
fail | Function | No | Callback function upon call failure. |
complete | Function | No | Callback function upon call completion (to be executed upon either call success or failure). |
Hide the toast dialog.
my.hideToast()
| Property | Type | Required | Description |
|---|---|---|---|
success | Function | No | The callback function for a successful API call. |
fail | Function | No | The callback function for a failed API call. |
complete | Function | No | The callback function used when the API call is completed. This function is always executed no matter the call succeeds or fails. |
The type parameter controls the icon displayed with the toast:
success - Shows a success checkmark iconfail - Shows a failure/error iconexception - Shows an exception icon (requires content to be specified)none - No icon, text onlyduration (default: 2000ms)my.hideToast() to dismiss a toast before its duration expirestype: 'exception', the content parameter is mandatorymy.showToast({
type: 'success',
content: 'Saved successfully',
duration: 2000,
});
my.showToast({
type: 'fail',
content: 'Failed to save',
duration: 3000,
});
my.showToast({
type: 'exception',
content: 'Network error occurred',
duration: 5000,
});
// Manually hide after 2 seconds
setTimeout(() => {
my.hideToast();
}, 2000);
my.showToast({
type: 'none',
content: 'Processing your request',
duration: 2000,
});
my.showToast({
type: 'success',
content: 'Operation completed',
duration: 2000,
success: () => {
console.log('Toast displayed');
// Navigate or perform next action
},
});
function submitForm() {
my.showLoading({
content: 'Submitting...',
});
// Submit form
fetch('/api/submit', {
method: 'POST',
body: JSON.stringify(formData),
})
.then(response => {
my.hideLoading();
if (response.ok) {
my.showToast({
type: 'success',
content: 'Form submitted successfully',
duration: 2000,
});
} else {
my.showToast({
type: 'fail',
content: 'Submission failed',
duration: 3000,
});
}
})
.catch(error => {
my.hideLoading();
my.showToast({
type: 'exception',
content: 'Network error',
duration: 3000,
});
});
}