Notification通知提醒框
在系统四个角显示通知提醒信息。经常用于以下情况:
- 较为复杂的通知内容。
- 带有交互的通知,给出用户下一步的行动点。
- 系统主动推送。
想要了解更多关于单独引入组件的内容,可以在快速上手页面进行查看。
与类似,如果要修改全局默认配置,你可以设置提供商 NZ_NOTIFICATION_CONFIG
的值来修改。(如:在你的模块的providers
中加入 { provide: NZ_NOTIFICATION_CONFIG, useValue: { nzDuration: 3000 }}
,NZ_NOTIFICATION_CONFIG
可以从 ng-zorro-antd
中导入)
默认全局配置为:
{
nzTop : '24px',
nzBottom : '24px',
nzPlacement : 'topRight',
nzDuration : 4500,
nzMaxStack : 7,
nzPauseOnHover: true,
nzAnimate : true
}
最简单的用法,4.5 秒后自动关闭。
import { Component } from '@angular/core';
import { NzNotificationService } from 'ng-zorro-antd';
@Component({
selector: 'nz-demo-notification-basic',
template: `
<button nz-button [nzType]="'primary'" (click)="createBasicNotification()">Open the notification box</button>
`
})
export class NzDemoNotificationBasicComponent {
constructor(private notification: NzNotificationService) {}
createBasicNotification(): void {
this.notification.blank(
'Notification Title',
'This is the content of the notification. This is the content of the notification. This is the content of the notification.'
);
}
}
通知提醒框左侧有图标。
import { Component } from '@angular/core';
import { NzNotificationService } from 'ng-zorro-antd';
@Component({
selector: 'nz-demo-notification-with-icon',
template: `
<button nz-button (click)="createNotification('success')">Success</button>
<button nz-button (click)="createNotification('info')">Info</button>
<button nz-button (click)="createNotification('warning')">Warning</button>
<button nz-button (click)="createNotification('error')">Error</button>
`,
styles: [
`
button {
margin-right: 1em;
}
`
]
})
export class NzDemoNotificationWithIconComponent {
createNotification(type: string): void {
this.notification.create(
type,
'Notification Title',
'This is the content of the notification. This is the content of the notification. This is the content of the notification.'
);
}
constructor(private notification: NzNotificationService) {}
}
使用 nzStyle 和 nzClass 来定义样式。
import { Component } from '@angular/core';
import { NzNotificationService } from 'ng-zorro-antd';
@Component({
selector: 'nz-demo-notification-custom-style',
<button nz-button [nzType]="'primary'" (click)="createBasicNotification()">Open the notification box</button>
`
})
export class NzDemoNotificationCustomStyleComponent {
constructor(private notification: NzNotificationService) {}
createBasicNotification(): void {
this.notification.blank(
'Notification Title',
'This is the content of the notification. This is the content of the notification. This is the content of the notification.',
{
nzStyle: {
width: '600px',
},
nzClass: 'test-class'
}
);
}
}
通过模板来实现更加复杂的效果和交互。
import { Component, TemplateRef, ViewChild } from '@angular/core';
import { NzNotificationService } from 'ng-zorro-antd';
@Component({
selector: 'nz-demo-notification-template',
template: `
<button nz-button [nzType]="'primary'" (click)="ninja()">Open the notification box</button>
<ng-template let-fruit="data">
It's a <nz-tag [nzColor]="fruit.color">{{ fruit.name }}</nz-tag>
<button nz-button nzType="small">Cut It!</button>
</ng-template>
`,
styles: [
`
button {
margin-top: 8px;
}
`
]
})
export class NzDemoNotificationTemplateComponent {
@ViewChild(TemplateRef, { static: false }) template: TemplateRef<{}>;
ninja(): void {
const fruits = [
{ name: 'Apple', color: 'red' },
{ name: 'Orange', color: 'orange' },
{ name: 'Watermelon', color: 'green' }
];
fruits.forEach(fruit => {
this.notificationService.template(this.template, { nzData: fruit });
});
}
constructor(private notificationService: NzNotificationService) {}
}
自定义通知框自动关闭的延时,默认4.5s
,取消自动关闭只要将该值设为 0
即可。
import { Component } from '@angular/core';
import { NzNotificationService } from 'ng-zorro-antd';
@Component({
selector: 'nz-demo-notification-duration',
template: `
<button nz-button [nzType]="'primary'" (click)="createBasicNotification()">Open the notification box</button>
`
})
export class NzDemoNotificationDurationComponent {
createBasicNotification(): void {
this.notification.blank(
'Notification Title',
'I will never close automatically. I will be close automatically. I will never close automatically.',
{ nzDuration: 0 }
);
}
constructor(private notification: NzNotificationService) {}
}
自定义关闭按钮的样式和文字。
可以设置通知从右上角、右下角、左下角、左上角弹出。
import { Component } from '@angular/core';
import { NzNotificationService } from 'ng-zorro-antd';
@Component({
selector: 'nz-demo-notification-placement',
template: `
<nz-select
[(ngModel)]="placement"
style="width: 120px; margin-right: 10px;"
(ngModelChange)="clearBeforeNotifications()"
>
<nz-option nzValue="topLeft" nzLabel="topLeft"></nz-option>
<nz-option nzValue="topRight" nzLabel="topRight"></nz-option>
<nz-option nzValue="bottomLeft" nzLabel="bottomLeft"></nz-option>
<nz-option nzValue="bottomRight" nzLabel="bottomRight"></nz-option>
</nz-select>
<button nz-button [nzType]="'primary'" (click)="createBasicNotification()">Open the notification box</button>
})
export class NzDemoNotificationPlacementComponent {
placement = 'topRight';
clearBeforeNotifications(): void {
this.notification.remove();
}
createBasicNotification(): void {
this.notification.config({
nzPlacement: this.placement
});
this.notification.blank(
'Notification Title',
'This is the content of the notification. This is the content of the notification. This is the content of the notification.'
);
}
constructor(private notification: NzNotificationService) {}
}
可以通过唯一的 nzKey
来更新内容。
import { Component } from '@angular/core';
import { NzNotificationService } from 'ng-zorro-antd';
@Component({
selector: 'nz-demo-notification-update',
template: `
<button nz-button [nzType]="'primary'" (click)="createAutoUpdatingNotifications()">
Open the notification box
</button>
`
})
export class NzDemoNotificationUpdateComponent {
constructor(private notification: NzNotificationService) {}
createAutoUpdatingNotifications(): void {
this.notification.blank('Notification Title', 'Description.', {
nzKey: 'key'
});
setTimeout(() => {
this.notification.blank('New Title', 'New description', {
nzKey: 'key'
});
}, 1000);
}
}
组件提供了一些服务方法,使用方式和参数如下:
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
或其他方法时会返回该对象。
export interface NzNotificationDataFilled {
onClose: Subject<boolean>; // 当 notification 关闭时它会派发一个事件,如果为用户手动关闭会派发 `true`