Angular 2 : Loading nested Component is destroying the existing scope of ng2-toastr

江枫思渺然 提交于 2019-12-12 02:16:37

问题


I'm using a ng2-toastr in my page and working fine, but when I have a nested component in the page the existing ng2-toastr(ToastManager) scope is destroyed and toastr is not working.

constructor(public toastr: ToastsManager,public vcr: ViewContainerRef) {
        this.toastr.setRootViewContainerRef(vcr);
}

In My method when I call

this.toastr.warning('Its Warning','Alert');

Its Working fine, but in my html when I'm loading other component i.e

<es-app></es-app>

the toastr in my page is not working (No errors)

Sometimes I get:

Attempt to use a destroyed view: detectChanges Error: Attempt to use a destroyed view: detectChanges at ViewDestroyedError


回答1:


By initializing the container inside ngAfterViewInit resolved the issue

this.toastr.setRootViewContainerRef(vcr);

instead of contsructor place in

ngAfterViewInit(){
this.toastr.setRootViewContainerRef(this.vcr);
}

Because the nested components are loading and destroying the page instance, So we have to load after all components are loaded and that happens in ngAfterViewInit as per page lifecycle hook



来源:https://stackoverflow.com/questions/42386249/angular-2-loading-nested-component-is-destroying-the-existing-scope-of-ng2-toa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!