Notification通知提醒框

    在系统四个角显示通知提醒信息。经常用于以下情况:

    • 较为复杂的通知内容。
    • 带有交互的通知,给出用户下一步的行动点。
    • 系统主动推送。

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

    与类似,如果要修改全局默认配置,你可以设置提供商 NZ_NOTIFICATION_CONFIG 的值来修改。(如:在你的模块的providers中加入 { provide: NZ_NOTIFICATION_CONFIG, useValue: { nzDuration: 3000 }}NZ_NOTIFICATION_CONFIG 可以从 ng-zorro-antd 中导入)

    默认全局配置为:

    1. {
    2. nzTop : '24px',
    3. nzBottom : '24px',
    4. nzPlacement : 'topRight',
    5. nzDuration : 4500,
    6. nzMaxStack : 7,
    7. nzPauseOnHover: true,
    8. nzAnimate : true
    9. }

    最简单的用法,4.5 秒后自动关闭。

    1. import { Component } from '@angular/core';
    2. import { NzNotificationService } from 'ng-zorro-antd';
    3. @Component({
    4. selector: 'nz-demo-notification-basic',
    5. template: `
    6. <button nz-button [nzType]="'primary'" (click)="createBasicNotification()">Open the notification box</button>
    7. `
    8. })
    9. export class NzDemoNotificationBasicComponent {
    10. constructor(private notification: NzNotificationService) {}
    11. createBasicNotification(): void {
    12. this.notification.blank(
    13. 'Notification Title',
    14. 'This is the content of the notification. This is the content of the notification. This is the content of the notification.'
    15. );
    16. }
    17. }

    Notification通知提醒框 - 图2

    带有图标的通知提醒框

    通知提醒框左侧有图标。

    1. import { Component } from '@angular/core';
    2. import { NzNotificationService } from 'ng-zorro-antd';
    3. @Component({
    4. selector: 'nz-demo-notification-with-icon',
    5. template: `
    6. <button nz-button (click)="createNotification('success')">Success</button>
    7. <button nz-button (click)="createNotification('info')">Info</button>
    8. <button nz-button (click)="createNotification('warning')">Warning</button>
    9. <button nz-button (click)="createNotification('error')">Error</button>
    10. `,
    11. styles: [
    12. `
    13. button {
    14. margin-right: 1em;
    15. }
    16. `
    17. ]
    18. })
    19. export class NzDemoNotificationWithIconComponent {
    20. createNotification(type: string): void {
    21. this.notification.create(
    22. type,
    23. 'Notification Title',
    24. 'This is the content of the notification. This is the content of the notification. This is the content of the notification.'
    25. );
    26. }
    27. constructor(private notification: NzNotificationService) {}
    28. }

    Notification通知提醒框 - 图4

    自定义样式

    使用 nzStyle 和 nzClass 来定义样式。

    1. import { Component } from '@angular/core';
    2. import { NzNotificationService } from 'ng-zorro-antd';
    3. @Component({
    4. selector: 'nz-demo-notification-custom-style',
    5. <button nz-button [nzType]="'primary'" (click)="createBasicNotification()">Open the notification box</button>
    6. `
    7. })
    8. export class NzDemoNotificationCustomStyleComponent {
    9. constructor(private notification: NzNotificationService) {}
    10. createBasicNotification(): void {
    11. this.notification.blank(
    12. 'Notification Title',
    13. 'This is the content of the notification. This is the content of the notification. This is the content of the notification.',
    14. {
    15. nzStyle: {
    16. width: '600px',
    17. },
    18. nzClass: 'test-class'
    19. }
    20. );
    21. }
    22. }

    通过模板来实现更加复杂的效果和交互。

    1. import { Component, TemplateRef, ViewChild } from '@angular/core';
    2. import { NzNotificationService } from 'ng-zorro-antd';
    3. @Component({
    4. selector: 'nz-demo-notification-template',
    5. template: `
    6. <button nz-button [nzType]="'primary'" (click)="ninja()">Open the notification box</button>
    7. <ng-template let-fruit="data">
    8. It's a <nz-tag [nzColor]="fruit.color">{{ fruit.name }}</nz-tag>
    9. <button nz-button nzType="small">Cut It!</button>
    10. </ng-template>
    11. `,
    12. styles: [
    13. `
    14. button {
    15. margin-top: 8px;
    16. }
    17. `
    18. ]
    19. })
    20. export class NzDemoNotificationTemplateComponent {
    21. @ViewChild(TemplateRef, { static: false }) template: TemplateRef<{}>;
    22. ninja(): void {
    23. const fruits = [
    24. { name: 'Apple', color: 'red' },
    25. { name: 'Orange', color: 'orange' },
    26. { name: 'Watermelon', color: 'green' }
    27. ];
    28. fruits.forEach(fruit => {
    29. this.notificationService.template(this.template, { nzData: fruit });
    30. });
    31. }
    32. constructor(private notificationService: NzNotificationService) {}
    33. }

    Notification通知提醒框 - 图6

    自动关闭的延时

    自定义通知框自动关闭的延时,默认4.5s,取消自动关闭只要将该值设为 0 即可。

    1. import { Component } from '@angular/core';
    2. import { NzNotificationService } from 'ng-zorro-antd';
    3. @Component({
    4. selector: 'nz-demo-notification-duration',
    5. template: `
    6. <button nz-button [nzType]="'primary'" (click)="createBasicNotification()">Open the notification box</button>
    7. `
    8. })
    9. export class NzDemoNotificationDurationComponent {
    10. createBasicNotification(): void {
    11. this.notification.blank(
    12. 'Notification Title',
    13. 'I will never close automatically. I will be close automatically. I will never close automatically.',
    14. { nzDuration: 0 }
    15. );
    16. }
    17. constructor(private notification: NzNotificationService) {}
    18. }

    自定义关闭按钮的样式和文字。

    Notification通知提醒框 - 图8

    可以设置通知从右上角、右下角、左下角、左上角弹出。

    1. import { Component } from '@angular/core';
    2. import { NzNotificationService } from 'ng-zorro-antd';
    3. @Component({
    4. selector: 'nz-demo-notification-placement',
    5. template: `
    6. <nz-select
    7. [(ngModel)]="placement"
    8. style="width: 120px; margin-right: 10px;"
    9. (ngModelChange)="clearBeforeNotifications()"
    10. >
    11. <nz-option nzValue="topLeft" nzLabel="topLeft"></nz-option>
    12. <nz-option nzValue="topRight" nzLabel="topRight"></nz-option>
    13. <nz-option nzValue="bottomLeft" nzLabel="bottomLeft"></nz-option>
    14. <nz-option nzValue="bottomRight" nzLabel="bottomRight"></nz-option>
    15. </nz-select>
    16. <button nz-button [nzType]="'primary'" (click)="createBasicNotification()">Open the notification box</button>
    17. })
    18. export class NzDemoNotificationPlacementComponent {
    19. placement = 'topRight';
    20. clearBeforeNotifications(): void {
    21. this.notification.remove();
    22. }
    23. createBasicNotification(): void {
    24. this.notification.config({
    25. nzPlacement: this.placement
    26. });
    27. this.notification.blank(
    28. 'Notification Title',
    29. 'This is the content of the notification. This is the content of the notification. This is the content of the notification.'
    30. );
    31. }
    32. constructor(private notification: NzNotificationService) {}
    33. }

    更新消息内容

    可以通过唯一的 nzKey 来更新内容。

    1. import { Component } from '@angular/core';
    2. import { NzNotificationService } from 'ng-zorro-antd';
    3. @Component({
    4. selector: 'nz-demo-notification-update',
    5. template: `
    6. <button nz-button [nzType]="'primary'" (click)="createAutoUpdatingNotifications()">
    7. Open the notification box
    8. </button>
    9. `
    10. })
    11. export class NzDemoNotificationUpdateComponent {
    12. constructor(private notification: NzNotificationService) {}
    13. createAutoUpdatingNotifications(): void {
    14. this.notification.blank('Notification Title', 'Description.', {
    15. nzKey: 'key'
    16. });
    17. setTimeout(() => {
    18. this.notification.blank('New Title', 'New description', {
    19. nzKey: 'key'
    20. });
    21. }, 1000);
    22. }
    23. }

    组件提供了一些服务方法,使用方式和参数如下:

    • NzNotificationService.blank(title, content, [options]) // 不带图标的提示
    • NzNotificationService.success(title, content, [options])
    • NzNotificationService.error(title, content, [options])
    • NzNotificationService.info(title, content, [options])
    • NzNotificationService.warning(title, content, [options])

    options 支持设置的参数如下:

    还提供了全局销毁方法:

    • NzNotificationService.remove(id) // 移除特定id的消息,当id为空时,移除所有消息(该消息id通过上述方法返回值中得到)

    当你调用 NzNotificationService.success 或其他方法时会返回该对象。

    1. export interface NzNotificationDataFilled {
    2. onClose: Subject<boolean>; // 当 notification 关闭时它会派发一个事件,如果为用户手动关闭会派发 `true`