📅  最后修改于: 2023-12-03 15:20:39.370000             🧑  作者: Mango
Toast Angular is a library for displaying toast notifications in Angular applications. Toast notifications are used to display important messages to users in a non-intrusive way. The library is built on top of Angular Material and provides a customizable and flexible way of displaying toasts.
To install Toast Angular, run the following command in your Angular project:
npm install @angular-toast/core @angular-toast/mat-toast
After installing the library, you need to import the ToastModule in your application module.
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ToastModule } from '@angular-toast/core';
import { MatToastModule } from '@angular-toast/mat-toast';
@NgModule({
imports: [
BrowserAnimationsModule,
ToastModule.forRoot(),
MatToastModule
],
bootstrap: [AppComponent]
})
export class AppModule { }
To display a toast notification, you need to inject the ToastService and call the show function with a message and an optional configuration object.
import { Component } from '@angular/core';
import { ToastService } from '@angular-toast/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {
constructor(private toastService: ToastService) { }
showToast() {
this.toastService.show('Hello, world!', { duration: 3000 });
}
}
In the above example, we are displaying a toast notification with a message 'Hello, world!' and a duration of 3000 milliseconds (3 seconds).
The Toast Angular library supports many configuration options that can be used to customize the appearance and behavior of toasts. Here are some of the commonly used configuration options.
The position option can be used to change the position of the toast notification. The default position is 'bottom'.
this.toastService.show('Hello, world!', { position: 'top' });
The duration option can be used to change the duration of the toast notification in milliseconds. The default duration is 5000 milliseconds (5 seconds).
this.toastService.show('Hello, world!', { duration: 3000 });
The class option can be used to add custom CSS classes to the toast notification. This can be useful for styling the toast notification to match the theme of your application.
this.toastService.show('Hello, world!', { class: 'my-toast' });
Toast Angular is a powerful library for displaying toast notifications in Angular applications. With its many configuration options, customization possibilities, and simple API, it can save you a lot of time and effort in displaying important messages to your users.