如何自定义 Toast window 的样式?

How to customize style of Toast window?

<p-toast>window的默认大小很小,我希望能够调整它,因为我的吐司消息可能会很长。我还需要能够自定义 toast 的样式 window,但我似乎无法让 CSS 工作。

this.messageService.addAll([
  {key: 'tc', severity: 'info', summary: '30 Nov 2020', detail: 'Very very very long task message to display number one that user needs to see blah blah blah'},
  {key: 'tc', severity: 'success', summary: '30 Nov 2020', detail: 'Very very very long task message to display number two that needs to be bigger and more prominent'}
]);

我已经根据文档尝试了几种方法。

内联html

<p-toast position="top-center" width="90%" key="tc"></p-toast>

使用 ngStyle

<p-toast position="top-center" [ngStyle]="{ 'width': '90%' }" key="tc"></p-toast>

CSS 组件

@Component({
  selector: 'task',
  templateUrl: './task.component.html',
  styleUrls: ['./task.component.css'],
  styles: [`
    :host ::ng-deep .custom-toast .ui-toast-message {
        color: #ffffff;
        width: 100%;
        background: #FFD07B;
    }` ],
  providers: [MessageService]
})


<p-toast position="top-center" style="custom-toast" key="tc"></p-toast>

添加到style.css

.custom-toast {
    width: 90%;
    background: #FFD07B;
}

:host ::ng-deep .custom-toast .ui-toast-message {
    width: 90%;
    background: #FFD07B;
}


<p-toast position="top-center" class="custom-toast" key="tc"></p-toast>

none 其中有效。

我是不是在语法的某个地方犯了错误?如何准确调整 Toast 的样式(颜色、字体,尤其是大小)window?

编辑:这是一个 stackblitz link,其中包含我尝试过的所有内容。不确定我是否做对了。

您的“.ui-toast-message”class 是 'display: block;' 类型。 您首先需要将其更改为 'display: flex;'

所以你的class可能看起来像

:host ::ng-deep .custom-toast .ui-toast-message {
    display: flex;
    width: 500px; // whatever width you need
}

和您的 angular 代码

<p-toast position="top-center" class="custom-toast" key="tc"></p-toast>

编辑:工作示例 StackBlitz

经过一番折腾,终于想出了解决办法。我需要在 styles.css.

中添加对 toast 容器的额外引用

html 基本保持不变。

<p-toast class="custom-toast"></p-toast>

Styles.css 更改为添加对 toast 容器的引用。

// centers the toast window to the middle of the screen
.custom-toast .ui-toast {
  position: fixed;
  top: 20%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 50%;
}

// adjusts font, color and other elements of the toast window if needed
.custom-toast .ui-toast-message-content {
  font-family: 'Courier New', Courier, monospace;
  background: #FFD07B;
}

我遇到了同样的问题,请像这样在您的代码中插入一个样式

   <style>
        .ui.toast {
            width: 500px!important;
        }
    </style>

记住你所有的 toast 都会有这个宽度,不使用时请移除。