Toast轻提示

  • 一次只显示一个 toast。
  • 有 Icon 的 Toast,字数为 4-6 个;没有 Icon 的 Toast,字数不宜超过 14 个。
  • this._toast.fail(content, duration, onClose, mask)
  • this._toast.info(content, duration, onClose, mask)
  • this._toast.loading(content, duration, onClose, mask)
  • this._toast.offline(content, duration, onClose, mask)

组件提供了五个静态方法,参数如下:

还提供了全局配置和全局销毁方法:

  • this._toast.hide()

代码演示

最简单的用法。

  1. import { Component } from '@angular/core';
  2. import { ToastService } from 'ng-zorro-antd-mobile';
  3. @Component({
  4. selector: 'demo-toast-basic',
  5. template: `
  6. <WingBlank>
  7. <div Button (onClick)="showToast()">text only</div>
  8. <WhiteSpace></WhiteSpace>
  9. <div Button (onClick)="showToastNoMask()">without mask</div>
  10. <WhiteSpace></WhiteSpace>
  11. <div Button (onClick)="showToastTop()">position top</div>
  12. <WhiteSpace></WhiteSpace>
  13. <div Button (onClick)="showToastBottom()">position bottom</div>
  14. <div Button (onClick)="showCustomIcon(content)">custom content</div>
  15. <WhiteSpace></WhiteSpace>
  16. <div Button (onClick)="successToast()">success</div>
  17. <div Button (onClick)="failToast()">fail</div>
  18. <WhiteSpace></WhiteSpace>
  19. <div Button (onClick)="offline()">network failure</div>
  20. <WhiteSpace></WhiteSpace>
  21. <div Button (onClick)="loadingToast()">loading</div>
  22. <WhiteSpace></WhiteSpace>
  23. <ng-template #content>
  24. <p>toast的内容</p>
  25. <p>toast的内容</p>
  26. </ng-template>
  27. </WingBlank>
  28. `
  29. })
  30. export class DemoToastBasicComponent {
  31. constructor(private _toast: ToastService) {}
  32. showToast() {
  33. const toast = this._toast.show('This is a toast tips !!!', 0);
  34. setTimeout(() => {
  35. this._toast.hide();
  36. }, 3000);
  37. }
  38. showToastNoMask() {
  39. const toast = this._toast.info('Toast without mask !!!', 4000, null, false);
  40. }
  41. }
  42. showToastBottom() {
  43. const toast = this._toast.info('Toast position top', 4000, null, false, 'bottom');
  44. }
  45. showCustomIcon(event) {
  46. const toast = this._toast.info(event);
  47. }
  48. successToast() {
  49. const toast = this._toast.success('Load success !!!', 3000, () => {
  50. console.log('success');
  51. });
  52. }
  53. failToast() {
  54. const toast = this._toast.fail('Load failed !!!', 1000);
  55. }
  56. offline() {
  57. const toast = this._toast.offline('Network connection failed !!!', 1000);
  58. }
  59. loadingToast() {
  60. const toast = this._toast.loading('Loading...', 3000, () => {
  61. console.log('Load complete !!!');
  62. });
  63. }