angular6 使用信息提示框toast


angular6 可以使用的toast插件有好多個,在目前來看ngx-toastr在過去一年時間的使用量和受歡迎程度可以說是一騎絕塵,如下圖:

我也就選擇了ngx-toastr這個插件,使用步驟如下:

1、安裝ngx-toastr和它的依賴@angular/animations

npm install ngx-toastr --save
npm install @angular/animations --save

 

2、在angular-cli.json中添加css樣式

"styles": [
  "styles.scss",
  "node_modules/ngx-toastr/toastr.css"
]

 

3、在app.module中導入相關模塊

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 {}

 

4、使用toast

import { ToastrService } from 'ngx-toastr';
 
@Component({...})
export class YourComponent {
  constructor(private toastr: ToastrService) {}
 
  showSuccess() {
    this.toastr.success('Hello world!', 'Toastr fun!');
  }
}

可以設置toast的消失時間為3秒:

this.toastr.success('Hello world!', 'Toastr fun!', {timeOut: 3000})

對toast做一些其他的配置可參考:https://github.com/scttcper/ngx-toastr#options


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM