Alert警告提示

警告提示,展现需要关注的信息。

何时使用

  • 当某个页面需要向用户显示警告的信息时。
  • 非浮层的静态展现形式,始终展现,不会自动消失,用户可以点击关闭。

单独引入此组件

想要了解更多关于单独引入组件的内容,可以在快速上手页面进行查看。

  1. import { NzAlertModule } from 'ng-zorro-antd/alert';

代码演示

Alert警告提示 - 图1

基本

最简单的用法,适用于简短的警告提示。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-alert-basic',
  4. template: `
  5. <nz-alert nzType="success" nzMessage="Success Text"></nz-alert>
  6. `
  7. })
  8. export class NzDemoAlertBasicComponent {}

Alert警告提示 - 图2

可关闭的警告提示

显示关闭按钮,点击可关闭警告提示。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-alert-closable',
  4. template: `
  5. <nz-alert
  6. nzType="warning"
  7. nzCloseable
  8. nzMessage="Warning Text Warning Text Warning Text Warning Text Warning Text Warning Text Warning Text"
  9. (nzOnClose)="afterClose()"
  10. >
  11. </nz-alert>
  12. <nz-alert
  13. nzType="error"
  14. nzCloseable
  15. nzMessage="Error Text"
  16. nzDescription="Error Description Error Description Error Description Error Description Error Description Error Description"
  17. (nzOnClose)="afterClose()"
  18. >
  19. </nz-alert>
  20. `,
  21. styles: [
  22. `
  23. nz-alert {
  24. margin-bottom: 16px;
  25. }
  26. `
  27. ]
  28. })
  29. export class NzDemoAlertClosableComponent {
  30. afterClose(): void {
  31. console.log('close');
  32. }
  33. }

Alert警告提示 - 图3

图标

可口的图标让信息类型更加醒目。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-alert-icon',
  4. template: `
  5. <nz-alert nzType="success" nzMessage="Success Tips" nzShowIcon></nz-alert>
  6. <nz-alert nzType="info" nzMessage="Informational Notes" nzShowIcon></nz-alert>
  7. <nz-alert nzType="warning" nzMessage="Warning" nzShowIcon></nz-alert>
  8. <nz-alert nzType="error" nzMessage="Error" nzShowIcon></nz-alert>
  9. <nz-alert
  10. nzType="success"
  11. nzMessage="Success Tips"
  12. nzDescription="Detailed description and advices about successful copywriting."
  13. nzShowIcon
  14. >
  15. </nz-alert>
  16. <nz-alert
  17. nzType="info"
  18. nzMessage="Informational Notes"
  19. nzDescription="Additional description and informations about copywriting."
  20. nzShowIcon
  21. >
  22. </nz-alert>
  23. <nz-alert
  24. nzType="warning"
  25. nzMessage="Warning"
  26. nzDescription="This is a warning notice about copywriting."
  27. nzShowIcon
  28. >
  29. </nz-alert>
  30. <nz-alert nzType="error" nzMessage="Error" nzDescription="This is an error message about copywriting." nzShowIcon>
  31. </nz-alert>
  32. `,
  33. styles: [
  34. `
  35. nz-alert {
  36. margin-bottom: 16px;
  37. }
  38. `
  39. ]
  40. })
  41. export class NzDemoAlertIconComponent {}

Alert警告提示 - 图4

顶部公告

页面顶部通告形式,默认有图标且nzType 为 'warning'。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-alert-banner',
  4. template: `
  5. <nz-alert nzBanner nzMessage="Warning text"></nz-alert>
  6. <nz-alert
  7. nzBanner
  8. nzMessage="Very long warning text warning text text text text text text text"
  9. nzCloseable
  10. ></nz-alert>
  11. <nz-alert nzBanner nzMessage="Warning text without icon" [nzShowIcon]="false"></nz-alert>
  12. <nz-alert nzBanner nzType="error" nzMessage="Error text"></nz-alert>
  13. `,
  14. styles: [
  15. `
  16. nz-alert {
  17. margin-bottom: 12px;
  18. }
  19. `
  20. ]
  21. })
  22. export class NzDemoAlertBannerComponent {}

Alert警告提示 - 图5

四种样式

共有四种样式 successinfowarningerror

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-alert-style',
  4. template: `
  5. <nz-alert nzType="success" nzMessage="Success Text"></nz-alert>
  6. <nz-alert nzType="info" nzMessage="Info Text"></nz-alert>
  7. <nz-alert nzType="warning" nzMessage="Warning Text"></nz-alert>
  8. <nz-alert nzType="error" nzMessage="Error Text"></nz-alert>
  9. `,
  10. styles: [
  11. `
  12. nz-alert {
  13. margin-bottom: 16px;
  14. }
  15. `
  16. ]
  17. })
  18. export class NzDemoAlertStyleComponent {}

Alert警告提示 - 图6

含有辅助性文字介绍

含有辅助性文字介绍的警告提示。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-alert-description',
  4. template: `
  5. <nz-alert
  6. nzType="success"
  7. nzMessage="Success Text"
  8. nzDescription="Success Description Success Description Success Description"
  9. >
  10. </nz-alert>
  11. <nz-alert
  12. nzType="info"
  13. nzMessage="Info Text"
  14. nzDescription="Info Description Info Description Info Description Info Description"
  15. >
  16. </nz-alert>
  17. <nz-alert
  18. nzType="warning"
  19. nzMessage="Warning Text"
  20. nzDescription="Warning Description Warning Description Warning Description Warning Description"
  21. >
  22. </nz-alert>
  23. <nz-alert
  24. nzType="error"
  25. nzMessage="Error Text"
  26. nzDescription="Error Description Error Description Error Description Error Description"
  27. >
  28. </nz-alert>
  29. `,
  30. styles: [
  31. `
  32. nz-alert {
  33. margin-bottom: 16px;
  34. }
  35. `
  36. ]
  37. })
  38. export class NzDemoAlertDescriptionComponent {}

Alert警告提示 - 图7

自定义关闭

可以自定义关闭,自定义的内容会替换原先的关闭按钮。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-alert-close-text',
  4. template: `
  5. <nz-alert nzType="info" nzMessage="Info Text" nzCloseText="Close Now"></nz-alert>
  6. `
  7. })
  8. export class NzDemoAlertCloseTextComponent {}

API

nz-alertcomponent

参数说明类型默认值
[nzBanner]是否用作顶部公告booleanfalse
[nzCloseable]默认不显示关闭按钮boolean-
[nzCloseText]自定义关闭按钮string | TemplateRef<void>-
[nzDescription]警告提示的辅助性文字介绍string | TemplateRef<void>-
[nzMessage]警告提示内容string | TemplateRef<void>-
[nzShowIcon]是否显示辅助图标,nzBanner 模式下默认值为 truebooleanfalse
[nzIconType]自定义图标类型,nzShowIcontrue 时有效string | string[] | Set<string> | { [klass: string]: any; }-
[nzType]指定警告提示的样式,nzBanner 模式下默认值为 'warning''success' | 'info' | 'warning' | 'error''info'
(nzOnClose)关闭时触发的回调函数EventEmitter<void>-