Toast轻提示

一种轻量级反馈/提示,可以用来显示不会打断用户操作的内容,适合用于页面转场、数据交互的等场景中。

规则

  • 一次只显示一个 toast。
  • 有 Icon 的 Toast,字数为 4-6 个;没有 Icon 的 Toast,字数不宜超过 14 个。

API 调用

  1. constructor(private _toast: ToastService) {}
  • this._toast.success(content, duration, onClose, mask)
  • 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)

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

参数说明类型默认值
[content]提示内容string | TemplateRef-
[duration]自动关闭的延时,单位毫秒number3000
[onClose]关闭后回调Function-
[mask]是否显示透明蒙层,防止触摸穿透booleantrue
[position]弹出的位置‘top’ | ‘middle’ | ‘bottom’‘middle’

注: duration = 0 时,onClose 无效,toast 不会消失;隐藏 toast 需要手动调用 hide

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

  • 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. <WhiteSpace></WhiteSpace>
  15. <div Button (onClick)="showCustomIcon(content)">custom content</div>
  16. <WhiteSpace></WhiteSpace>
  17. <div Button (onClick)="successToast()">success</div>
  18. <WhiteSpace></WhiteSpace>
  19. <div Button (onClick)="failToast()">fail</div>
  20. <WhiteSpace></WhiteSpace>
  21. <div Button (onClick)="offline()">network failure</div>
  22. <WhiteSpace></WhiteSpace>
  23. <div Button (onClick)="loadingToast()">loading</div>
  24. <WhiteSpace></WhiteSpace>
  25. <ng-template #content>
  26. <p>toast的内容</p>
  27. <p>toast的内容</p>
  28. </ng-template>
  29. </WingBlank>
  30. `
  31. })
  32. export class DemoToastBasicComponent {
  33. constructor(private _toast: ToastService) {}
  34. showToast() {
  35. const toast = this._toast.show('This is a toast tips !!!', 0);
  36. setTimeout(() => {
  37. this._toast.hide();
  38. }, 3000);
  39. }
  40. showToastNoMask() {
  41. const toast = this._toast.info('Toast without mask !!!', 4000, null, false);
  42. }
  43. showToastTop() {
  44. const toast = this._toast.info('Toast position top', 4000, null, false, 'top');
  45. }
  46. showToastBottom() {
  47. const toast = this._toast.info('Toast position top', 4000, null, false, 'bottom');
  48. }
  49. showCustomIcon(event) {
  50. const toast = this._toast.info(event);
  51. }
  52. successToast() {
  53. const toast = this._toast.success('Load success !!!', 3000, () => {
  54. console.log('success');
  55. });
  56. }
  57. failToast() {
  58. const toast = this._toast.fail('Load failed !!!', 1000);
  59. }
  60. offline() {
  61. const toast = this._toast.offline('Network connection failed !!!', 1000);
  62. }
  63. loadingToast() {
  64. const toast = this._toast.loading('Loading...', 3000, () => {
  65. console.log('Load complete !!!');
  66. });
  67. }
  68. }

Toast轻提示 - 图1