我在我的Angular 7应用程序中使用了以下toastr实现:https://www.npmjs.com/package/ngx-toastr
我正在尝试弄清楚如何将所有toast附加到body或其他div元素,这些元素将位于我的根应用程序组件中(我希望即使在调用它们的组件被销毁的情况下也能保持它们的显示)。
有没有办法把它归档?
发布于 2019-04-02 21:05:10
正如链接中的自述文件所述,您需要提供自己的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');
}
}发布于 2019-04-02 20:53:53
在根模块上声明模块(通常是app.module.ts)
import { ToastrModule } from 'ngx-toastr';
@NgModule({
imports: [ ToastrModule.forRoot({ ...global options... }) ],
...
})toasts可以在任何地方被调用(假设你已经在组件中注入了服务),并且应该在你定义它们的地方显示(并且没有元素覆盖它们)。
https://stackoverflow.com/questions/55475175
复制相似问题