How to create a SnackBar service? Last Updated : 14 May, 2020 Comments Improve Suggest changes Like Article Like Report MatSnackBar is an angular directive that's used for showing a notification bar specifically on the mobile devices. These types of UI components are generally used several times. So to avoid the repetition of code, a service can simply be created to use SnackBar in different components. Approach: To create a service you have to use the following command: ng g s snackBar Now import MatSnackBar from @angular/core and define the function openSnackBar (you can always use a different name). javascript import { Injectable } from '@angular/core'; import {MatSnackBar} from '@angular/material/snack-bar'; @Injectable({ providedIn: 'root' }) export class SnackBarService { //create an instance of MatSnackBar constructor(private snackBar:MatSnackBar) { } /* It takes three parameters 1.the message string 2.the action 3.the duration, alignment, etc. */ openSnackBar(message: string, action: string) { this.snackBar.open(message, action, { duration: 2000, }); } } Import the snackBarService and inject it inside the constructor of the component, in which you want to use the Snackbar. This will create an instance of the service say snackBService. Now call the openSnackBar function wherever it is required, with the help of snackBService. javascript import { Component, OnInit } from '@angular/core'; import {SnackBarService} from '../snack.service'; @Component({ selector: 'app-profile', templateUrl: './snackBar.html', styleUrls: ['./snackBar.css'] }) export class SnackBar { // create an instance of SnackBarService constructor(private snackBService:SnackBarService) { } //defining method for display of SnackBar trigger(message:string, action:string) { this.snackBService.openSnackBar(message, action); } } By repeating these steps we can use the snackBar inside any component. Example: html <button (click)="trigger('This is a ', 'SnackBar')"> SnackBarButton </button> Output: Comment More infoAdvertise with us Next Article How to use SnackBar Component in ReactJS ? V vaibhav19verma Follow Improve Article Tags : Web Technologies AngularJS AngularJS-Misc Similar Reads How to create a Snackbar using HTML, CSS & JavaScript? Snackbar or toast notifications are brief messages that pop up on a webpage to convey information or feedback to users. They are versatile tools for signaling different states, like success, error, or invalid input, and can appear at various positions on the screen.Approach:First, create a basic HTM 3 min read How to Create a Snackbar or Toast using CSS & JavaScript? Snackbar or toast notifications are brief messages that pop up on a webpage to convey information or feedback to users. They are versatile tools for signaling different states, like success, error, or invalid input, and can appear at various positions on the screen.Snackbar or Toast ApproachFirst, c 4 min read How to use SnackBar Component in ReactJS ? ReactJS provides developers with a wide range of components to create interactive and dynamic web applications. One such component is the SnackBar, which is commonly used to display temporary messages or notifications to users. Creating a SnackBar component allows for the presentation of these messa 2 min read How to create toast in React Native ? React Native is an open-source UI software framework created by Meta Platforms, Inc. It is used to develop applications for Android, Android TV, iOS, macOS, tvOS, Web, Windows, and UWP by enabling developers to use the React framework along with native platform capabilities.In this article, we will 2 min read React MUI SnackbarContent API Material-UI is a UI library providing predefined robust and customizable components for React for easier web development. The MUI design is based on top of Material Design by Google. In this article, we will discuss the React MUI SnackbarContent API. The Snackbars are used to provide brief notificat 3 min read React MUI Snackbar Feedback React Material UIÂ is an open-source library for the React User Interface components that implement Google's Material Design. It provides a wide range collection of prebuilt, reusable, responsive components which requires less coding and are ready to use for production implementation. It includes be 4 min read Like