Как уже указано в файле readme в вашей ссылке, вам необходимо предоставить собственный ToastrContainer.
import {
ToastrModule,
ToastContainerModule // Add this one
} from 'ngx-toastr';
@NgModule({
declarations: [AppComponent],
imports: [
//...
ToastContainerModule // Add this one
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}
И добавьте div к своему корневому компоненту (или к тому месту, где вы хотите, чтобы контейнер находился) следующим образом:
@Component({
selector: 'app-root',
template: `
<h1><a (click)="onClick()">Click</a></h1>
<div toastContainer></div> <!-- Add this line here, above should be your router -->
`
})
export class AppComponent implements OnInit {
// Get a reference to the directive
@ViewChild(ToastContainerDirective) toastContainer: ToastContainerDirective;
constructor(private toastrService: ToastrService) {}
ngOnInit() {
// Register the container
this.toastrService.overlayContainer = this.toastContainer;
}
onClick() {
this.toastrService.success('in div');
}
}