Lightning Web components(LWC) Toast Messages

Let’s discuss here how to use the toast notification in Lightning web component.  A component can send a toast notification that pops up to alert users of a success, error, or warning. A toast can also simply provide information. To display a toast notification in Lightning Experience or Lightning communities, import ShowToastEvent from the lightning-platform-show-toast-event module. You can style a toast to provide information, an error, a success, or a warning. You can also configure the visibility of the toast. It can remain visible for three seconds, until the user clicks to dismiss it, or a combination of both. To trigger a toast from a Lightning web component, in the component’s JavaScript class, import ShowToastEventfrom lightning/platformShowToastEvent. Create a ShowToastEvent with a few parameters, and dispatch it. The app handles the rest.

1. Import ShowToastEvent from lightning/platformShowToastEvent. Create and dispatch a ShowToastEvent event with titlemessagevariant, and mode properties.

2. Create a ShowToastEvent with a few parameters, and dispatch it

 

Show Error Toast 

use this code to show the error toast message

Show Warning Toast 

Use this code to show the warning Toast

Show Success Toast 

Use this code to show the success toast message

Show Info Toast 

Use this code to show the info toast

Here is the complete code the custom lightning web component .create a new lightning web component using the SFDX command

Here is the Notification.html code

Here is the Notification.js JavaScript controller

Use this Notification.js-meta.xml configuration files

Push the changes to scratch org and add this component to the record page and you can able to see the different toast notification based on the button click.