Уведомления
PrimeNG/Toast (opens in a new tab)
Toast используется для отображения всплывающих уведомлений.
С чего начать
Подключите модуль
import { ToastModule } from 'primeng/toast';
В .ts файле
import { Component, OnInit } from '@angular/core';
import { MessageService } from 'primeng/api';
@Component({
selector: 'app-prime-toast',
templateUrl: './prime-toast.component.html',
styleUrls: ['./prime-toast.component.scss'],
providers: [MessageService]
})
export class PrimeToastComponent implements OnInit {
constructor(private messageService: MessageService) { }
showSuccess() {
this.messageService.add({ severity: 'success', summary: 'Success', detail: 'Message Content' });
}
showInfo() {
this.messageService.add({ severity: 'info', summary: 'Info', detail: 'Message Content' });
}
showWarn() {
this.messageService.add({ severity: 'warn', summary: 'Warn', detail: 'Message Content' });
}
showError() {
this.messageService.add({ severity: 'error', summary: 'Error', detail: 'Message Content' });
}
showCustom() {
this.messageService.add({ severity: 'custom', summary: 'Custom', detail: 'Message Content', icon: 'pi-file' });
}
showTopLeft() {
this.messageService.add({ key: 'tl', severity: 'info', summary: 'Info', detail: 'Message Content' });
}
showTopCenter() {
this.messageService.add({ key: 'tc', severity: 'warn', summary: 'Warn', detail: 'Message Content' });
}
showBottomCenter() {
this.messageService.add({ key: 'bc', severity: 'success', summary: 'Success', detail: 'Message Content' });
}
showConfirm() {
this.messageService.clear();
this.messageService.add({ key: 'c', sticky: true, severity: 'warn', summary: 'Are you sure?', detail: 'Confirm to proceed' });
}
showMultiple() {
this.messageService.addAll([
{ severity: 'success', summary: 'Message 1', detail: 'Message Content' },
{ severity: 'info', summary: 'Message 2', detail: 'Message Content' },
{ severity: 'warn', summary: 'Message 3', detail: 'Message Content' }
]);
}
showSticky() {
this.messageService.add({ severity: 'info', summary: 'Sticky', detail: 'Message Content', sticky: true });
}
onConfirm() {
this.messageService.clear('c');
}
onReject() {
this.messageService.clear('c');
}
clear() {
this.messageService.clear();
}
ngOnInit(): void {
}
}
Встройте компонент с помощью тэга p-toast.
Размещение кода
Для отображения всплывающих сообщений необходимо разместить следующий код в теле страницы:
Severities
Positions
Multiple
Clear
Template
Свойства
Name | Type | Default | Description |
---|---|---|---|
key | string | null | Key to match the key of a message to display. |
style | string | null | Inline style of the component. |
styleClass | string | null | Style class of the component. |
position | string | top-right | Position of the component, valid values are "top-right", "top-left", "bottom-left", "bottom-right", "top-center, "bottom-center" and "center". |
baseZIndex | number | 0 | Base zIndex value to use in layering. |
autoZIndex | boolean | true | Whether to automatically manage layering. |
showTransitionOptions | string | 300ms ease-out | Transition options of the show animation. |
hideTransitionOptions | string | 250ms ease-in | Transition options of the hide animation. |
showTransformOptions | string | translateY(100%) | Transform options of the show animation. |
hideTransformOptions | string | translateY(-100%) | Transform options of the hide animation. |
preventOpenDuplicates | boolean | false | It does not add the new message if there is already a toast displayed with the same content |
breakpoints | object | null | Object literal to define styles per screen size. |
preventDuplicates | boolean | false | Displays only once a message with the same content. |
События
Name | Parameters | Description |
---|---|---|
onClose | event.message: Removed message | Callback to invoke when a message is closed. |
Шаблоны
Name | Parameters |
---|---|
message | - |
Стилизация
Name | Element |
---|---|
p-toast | Main container element. |
p-toast-message | Container of a message item. |
p-toast-icon-close | Close icon of a message. |
p-toast-icon | Severity icon. |
p-toast-message-content | Container of message texts. |
p-toast-title | Summary of the message. |
p-toast-title | Detail of the message. |