angular6 能够使用的toast插件有好多个,在目前来看ngx-toastr在过去一年时间的使用量和受欢迎程度能够说是一骑绝尘,以下图:css
我也就选择了ngx-toastr这个插件,使用步骤以下:node
一、安装ngx-toastr和它的依赖@angular/animationsgit
npm install ngx-toastr --save npm install @angular/animations --save
二、在angular-cli.json中添加css样式github
"styles": [ "styles.scss", "node_modules/ngx-toastr/toastr.css" ]
三、在app.module中导入相关模块npm
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { ToastrModule } from 'ngx-toastr';
import { AppComponent } from './app.component'; @NgModule({ imports: [ BrowserAnimationsModule, ToastrModule.forRoot() ], bootstrap: [AppComponent], declarations: [AppComponent] }) class AppModule {}
四、使用toastjson
import { ToastrService } from 'ngx-toastr'; @Component({...}) export class YourComponent { constructor(private toastr: ToastrService) {} showSuccess() { this.toastr.success('Hello world!', 'Toastr fun!'); } }
能够设置toast的消失时间为3秒:bootstrap
this.toastr.success('Hello world!', 'Toastr fun!', {timeOut: 3000})
对toast作一些其余的配置可参考:https://github.com/scttcper/ngx-toastr#optionsapp