gpt4 book ai didi

angular - 应用程序组件中的 prime-ng p-toast 不显示从其他组件发送的消息

转载 作者:行者123 更新时间:2023-12-03 14:01:23 25 4
gpt4 key购买 nike

为了减少我的 webapp 中的primeng toast 组件(p-toast)的数量,我尝试在app.component 中使用带有key 的中央p-toast。然后我使用消息服务和该 toast 组件的键添加来自其他组件的消息。不幸的是,没有显示 toast 。

我的 app.component.html

<div>
<app-header></app-header>
<router-outlet></router-outlet>
<app-footer *ngIf="!userLoggedIn"></app-footer>
</div>
<p-toast [style]="{ marginTop: '80px' }" key="myToast"></p-toast>

我的 app.component.ts
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
providers: [MessageService]
})
export class AppComponent implements OnInit, OnDestroy {

constructor(
private readonly messageService: MessageService
) {...

从组件内(通过路由器 socket 显示)我添加了一条消息:
  this.messageService.add({
severity: 'success',
summary: 'Success Message',
key: 'myToast',
detail: 'Order submitted'
});

我还尝试了以下替代方案:
this.ngZone.run(() => {
this.messageService.add({
severity: 'success',
summary: 'Success Message',
key: 'myToast',
detail: 'Order submitted'
});
});


setTimeout(() => {
this.messageService.add({
severity: 'success',
summary: 'Success Message',
key: 'myToast',
detail: 'Order submitted'
});
}, 1000);

这些都不起作用。

我忘记了什么吗?或者 p-toast 不能这样使用?

最佳答案

你只需要改变一些东西

1) 将 ToastModule 添加到 AppModule 导入列表中

2) 将 MessageService 添加到 AppModule 提供者列表中

3) 在任何其他组件中从 providers 数组中删除 MessageService,您仍然可以导入 MessageService 并执行 this.messageService.add从任何组件

然后试试看

关于angular - 应用程序组件中的 prime-ng p-toast 不显示从其他组件发送的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55828970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com