Angular 角度2:加载嵌套组件正在破坏ng2 toastr的现有范围

Angular 角度2:加载嵌套组件正在破坏ng2 toastr的现有范围,angular,toastr,angular-toastr,Angular,Toastr,Angular Toastr,我在我的页面中使用了ng2 toastr,工作正常,但是当我在页面中有嵌套组件时,现有的ng2 toastr(ToastManager)作用域被破坏,toastr不工作 constructor(public toastr: ToastsManager,public vcr: ViewContainerRef) { this.toastr.setRootViewContainerRef(vcr); } 在我的方法中,当我调用 this.toastr.warning('Its Wa

我在我的页面中使用了ng2 toastr,工作正常,但是当我在页面中有嵌套组件时,现有的ng2 toastr(ToastManager)作用域被破坏,toastr不工作

constructor(public toastr: ToastsManager,public vcr: ViewContainerRef) {
        this.toastr.setRootViewContainerRef(vcr);
}
在我的方法中,当我调用

this.toastr.warning('Its Warning','Alert');
它工作正常,但在我的html中,当我加载其他组件时,例如

<es-app></es-app>

我的页面中的toastr不工作(无错误)

有时我会:

尝试使用已销毁视图:detectChanges错误:尝试使用已销毁视图:detectChanges at ViewDestroyedError


通过初始化ngAfterViewInit中的容器,解决了该问题

this.toastr.setRootViewContainerRef(vcr);
而不是将contsructor放入

ngAfterViewInit(){
this.toastr.setRootViewContainerRef(this.vcr);
}
因为嵌套组件正在加载和销毁页面实例,所以我们必须在加载所有组件之后加载,这在ngAfterViewInit中按照页面生命周期挂钩进行