Message全局提示

    • 可提供成功、警告和错误等反馈信息。
    • 顶部居中显示并自动消失,是一种不打断用户操作的轻量级提示方式。

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

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

    默认全局配置为:

    1. {
    2. nzDuration: 3000,
    3. nzMaxStack: 7,
    4. nzPauseOnHover: true,
    5. nzAnimate: true
    6. }

    信息提醒反馈。

    Message全局提示 - 图2

    修改延时

    1. import { Component } from '@angular/core';
    2. import { NzMessageService } from 'ng-zorro-antd/message';
    3. @Component({
    4. selector: 'nz-demo-message-duration',
    5. template: `
    6. `
    7. })
    8. export class NzDemoMessageDurationComponent {
    9. createBasicMessage(): void {
    10. this.message.success('This is a prompt message for success, and it will disappear in 10 seconds', {
    11. nzDuration: 10000
    12. });
    13. constructor(private message: NzMessageService) {}
    14. }

    可通过订阅 onClose 事件在 message 关闭时做出某些操作。以上用例将依次打开三个 message。

    Message全局提示 - 图4

    其他提示类型

    包括成功、失败、警告。

    1. import { Component } from '@angular/core';
    2. import { NzMessageService } from 'ng-zorro-antd/message';
    3. @Component({
    4. selector: 'nz-demo-message-other',
    5. template: `
    6. <button nz-button (click)="createMessage('success')">Success</button>
    7. <button nz-button (click)="createMessage('error')">Error</button>
    8. <button nz-button (click)="createMessage('warning')">Warning</button>
    9. `,
    10. styles: [
    11. [nz-button] {
    12. margin-right: 8px;
    13. }
    14. `
    15. ]
    16. })
    17. export class NzDemoMessageOtherComponent {
    18. createMessage(type: string): void {
    19. this.message.create(type, `This is a message of ${type}`);
    20. }
    21. constructor(private message: NzMessageService) {}
    22. }

    进行全局 loading,异步自行移除。

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

    • NzMessageService.success(content, [options])
    • NzMessageService.error(content, [options])
    • NzMessageService.info(content, [options])
    • NzMessageService.warning(content, [options])
    • NzMessageService.loading(content, [options])

    options 支持设置的参数如下:

    还提供了全局销毁方法:

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

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

    1. export interface NzMessageDataFilled {
    2. }