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 nzType="warning" nzMessage="Warning" nzDescription="This is a warning notice about copywriting." nzShowIcon> </nz-alert>
  24. <nz-alert nzType="error" nzMessage="Error" nzDescription="This is an error message about copywriting." nzShowIcon> </nz-alert>
  25. `,
  26. styles: [
  27. `
  28. nz-alert {
  29. margin-bottom: 16px;
  30. }
  31. `
  32. ]
  33. })
  34. 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 nzBanner nzMessage="Very long warning text warning text text text text text text text" nzCloseable></nz-alert>
  7. <nz-alert nzBanner nzMessage="Warning text without icon" [nzShowIcon]="false"></nz-alert>
  8. <nz-alert nzBanner nzType="error" nzMessage="Error text"></nz-alert>
  9. `,
  10. styles: [
  11. `
  12. nz-alert {
  13. margin-bottom: 12px;
  14. }
  15. `
  16. ]
  17. })
  18. 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 nzType="success" nzMessage="Success Text" nzDescription="Success Description Success Description Success Description">
  6. </nz-alert>
  7. <nz-alert nzType="info" nzMessage="Info Text" nzDescription="Info Description Info Description Info Description Info Description">
  8. </nz-alert>
  9. <nz-alert
  10. nzType="warning"
  11. nzMessage="Warning Text"
  12. nzDescription="Warning Description Warning Description Warning Description Warning Description"
  13. >
  14. </nz-alert>
  15. <nz-alert nzType="error" nzMessage="Error Text" nzDescription="Error Description Error Description Error Description Error Description">
  16. </nz-alert>
  17. `,
  18. styles: [
  19. `
  20. nz-alert {
  21. margin-bottom: 16px;
  22. }
  23. `
  24. ]
  25. })
  26. 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-
[nzType]指定警告提示的样式,nzBanner 模式下默认值为 ‘warning’‘success’ | ‘info’ | ‘warning’ | ‘error’‘info’
(nzOnClose)关闭时触发的回调函数EventEmitter<void>-