Problem
Displaying toast notifications in LWC requires importing the ShowToastEvent and dispatching it. This can become repetitive. A centralized service makes the code cleaner and easier to manage.
Solution Overview
This is a service component (no UI) that exposes simple methods like showSuccess(message), showError(message), etc. Your components can import this service and call these methods without needing to handle the ShowToastEvent logic themselves.
How to Deploy
- Deploy the LWC service component bundle.
- In your target LWC, import the service:
import { toastService } from 'c/toastService'; - Call the methods, e.g.,
toastService.showSuccess('Record saved!');
