简体   繁体   English

如何在页面加载时触发 Toast?

[英]How to trigger Toast on page load?

I am trying to use a <p-toast> in my landing page to display notification messages when users login.我试图在我的登录页面中使用<p-toast>来在用户登录时显示通知消息。

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

The messages are then declared in my components.然后在我的组件中声明消息。

ngOnInit() {

    // add toasts
    this.messageService.addAll([
      {key: 'tc', severity: 'info', summary: '30 Nov 2020', detail: 'Important message 1'},
      {key: 'tc', severity: 'info', summary: '30 Nov 2020', detail: 'Important message 2'}
    ]);

    // do other stuff

}

This doesn't work.这不起作用。 To confirm that it is not a problem with my toast or messages, I placed the messages inside an onClick() event on the page and manually triggered it, which does work.为了确认我的吐司或消息没有问题,我将消息放在页面上的onClick()事件中并手动触发它,这确实有效。

How do I get the Toasts to trigger on page load instead?如何让 Toast 在页面加载时触发?

Try using the AfterViewInit 尝试使用AfterViewInit

The issue might be because the HTML is not yet rendered so try the following. 问题可能是因为HTML尚未呈现,请尝试以下操作。 This is an Angular Life Cycle hook that is only fired when the HTML has been initialized. 这是一个Angular生命周期钩子,仅在初始化HTML时触发。

Be sure to add it to the class you are using it in like in the code snippet below, continue to use the ngOnInit for other logic that does not require HTML to be initialized and accessable. 请务必将其添加到您正在使用它的类中,如下面的代码片段所示,继续将ngOnInit用于其他不需要HTML初始化和可访问的逻辑。

class MyComponent implements AfterViewInit, ngOnInit {

  ngAfterViewInit() {
    this.messageService.addAll(
    [{key: 'tc', severity: 'info',
    summary: '30 Nov 2020', detail: 'Important message 1'},
    {key: 'tc', severity: 'info',
    summary: '30 Nov 2020', detail: 'Important message 2'}]);
  }

  ngOnInit() {
  //for other logic that does not require the HTML to be initialized.
  }

}

If you are having any issues with ExpressionHasChangedAfterItHasBeenCheckedError then you could alternatively use a different life cycle hook that looks for the view to be initialized. 如果您遇到ExpressionHasChangedAfterItHasBeenCheckedError任何问题,那么您也可以使用不同的生命周期钩子来查找要初始化的视图。

This would be AfterContentInit and would be used exactly the same as the above example. 这将是AfterContentInit,并且将与上面的示例完全相同。 Whichever works best for you given example. 以最适合你的方式为例。 If the easiest solution is to setTimeout then go with that if it causes you no other issues. 如果最简单的解决方案是setTimeout,那么请继续使用它,如果它没有引起其他问题。 Would suggest using Angular to your advantage though whilst using the framework. 建议在使用框架时使用Angular。

You have to send the messages once the view is initialized. 初始化视图后,您必须发送消息。

However, by triggering the messages in the ngAfterViewInit hook, you might get an ExpressionChangedAfterItHasBeenCheckedError . 但是,通过触发ngAfterViewInit挂钩中的消息,您可能会得到ExpressionChangedAfterItHasBeenCheckedError

Here are solutions to avoid this error: 以下是避免此错误的解决方案:

First solution is using a setTimeout call to send the messages once everything is initialized. 第一个解决方案是使用setTimeout调用在初始化所有内容后发送消息。

Here is a simple Stackblitz example 这是一个简单的Stackblitz示例

ngAfterViewInit() {
    setTimeout(() => {
        this.messageService.addAll([
            {key: 'tc', severity: 'info', summary: '30 Nov 2020', detail: 'Important message 1'},
            {key: 'tc', severity: 'info', summary: '30 Nov 2020', detail: 'Important message 2'}
        ]);
    })
}

Another solution is to force change detection using the ChangeDetectorRef ( see this article ): 另一种解决方案是使用ChangeDetectorRef强制进行更改检测( 请参阅此文章 ):

constructor(private messageService: MessageService, private cd: ChangeDetectorRef) { }

ngAfterViewInit() {
    this.messageService.addAll([
        {key: 'tc', severity: 'info', summary: '30 Nov 2020', detail: 'Important message 1'},
        {key: 'tc', severity: 'info', summary: '30 Nov 2020', detail: 'Important message 2'}
    ]);
    this.cd.detectChanges()
}

I believe that anytime you fire off some task such as this from the ngOnInit, for example a toast message or other async call that need to wait for the Init to finish you can put your call right inline with the rest of your code as long as you use setTimeout.我相信,无论何时您从 ngOnInit 中触发一些诸如此类的任务,例如吐司消息或其他需要等待 Init 完成的异步调用,您都可以将调用与其余代码直接内联你使用 setTimeout。 The ngAfterInit is not needed.不需要 ngAfterInit。 You can thank (or curse) single threaded JavaScript for that.你可以为此感谢(或诅咒)单线程 JavaScript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM