VOOZH about

URL: https://www.geeksforgeeks.org/angular-js/how-to-create-a-snackbar-service/

⇱ How to create a SnackBar service? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to create a SnackBar service?

Last Updated : 14 May, 2020
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).
  • 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.
  • By repeating these steps we can use the snackBar inside any component. Example:
  • Output: 👁 Image
    Comment
    Article Tags:

    Explore