akserg / ng2-toasty

Angular2 Toasty component shows growl-style alerts and messages for your app.
MIT License
283 stars 102 forks source link

Calling toast on ngOnInit don't work #74

Closed alienriquebm closed 7 years ago

alienriquebm commented 7 years ago

Bug

ngOnInit(){
          this.route.params.subscribe(params => {
            let value = params['logoutOperation'];
            this.operation = value || '';
            this.toastyService.error("Hi youre logoff"); 
          });
    }

On the constructor:

private toastyService:ToastyService

Zheness commented 7 years ago

Hello, same problem here. I'm on Angular4.

When I call the service in my onInit component, the toasty is not displayed.

I use this code:

ngOnInit(): void {
  this._toastyConfig.theme = 'bootstrap';
  let toastOptions: ToastOptions = {
    title: 'title',
    msg: 'my message',
    showClose: true,
    timeout: 15000,
    theme: "bootstrap",
    onAdd: (toast: ToastData) => {
      console.log('Toast ' + toast.id + ' has been added!');
    },
    onRemove: function (toast: ToastData) {
      console.log('Toast ' + toast.id + ' has been removed!');
    }
  };
  this._toastyService.success(toastOptions);
};

In the console we can see that the onAdd console.log() message is displayed but the toasty doesn't appear and no error in the console. And after 15 seconds (my config), no other console.log() is called from onRemove.

Thanks

alienriquebm commented 7 years ago

Well... we need to wait for @akserg if can help us with this problem.

akserg commented 7 years ago

Hi All,

You should use the AfterViewInit.ngAfterViewInit instead of OnInit.ngOnInit. Configuring of the Toasty better leave in the constructor or ngOnInit methods.

Check proof of concept in the Plunkr