angulartypescriptangular2-toaster

Error: No Toaster Containers have been initialized to receive toasts


I'm using angular2-toaster in my angular 6 app will update it to 9 later using this schema :

|___ app.module
|___ shared
     |_______shared.module
     |_______component
             |__________base.component.ts
|___ path
     |_______histograme.component

but I'm getting this error No Toaster Containers have been initialized to receive toasts. here is my histograme.component.ts

import { BaseComponent } from '../../shared/component/base-component';
export class histograme extends BaseComponent implements OnInit, OnDestroy {
...
error => this.showError(..)
}
...

for the baseComponent in which I loaded the ToasterModule and Service

import { ToasterService, ToasterConfig } from "angular2-toaster";

@Component({
  template: 
  <toaster-container></toaster-container>
})
export class BaseComponent implements OnDestroy {
protected toasterService: ToasterService;
  protected toasterconfig: ToasterConfig = new ToasterConfig({
    positionClass: 'toast-top-right',
    showCloseButton: true
  });
protected ShowError(){
      this.toasterService.pop('error', 'Erreur', 'message');
}
}

my shared.module :

   imports : [ToasterModule.forRoot()],
   providers: [ToasterService],
   exports: [ToasterModule]

app.module

   imports : [ToasterModule.forRoot()],
   providers: [ToasterService],

Solution

  • Hi add this in your html file where you want to show toaster.

    <toaster-container></toaster-container>
    
    
       private toasterService: ToasterService;
    
        constructor(toasterService: ToasterService) {
            this.toasterService = toasterService;
        }