Angular 错误:ViewDestroyedError:尝试使用已销毁的视图:检测规则中的更改

Angular 错误:ViewDestroyedError:尝试使用已销毁的视图:检测规则中的更改,angular,ngx-bootstrap,angular-changedetection,Angular,Ngx Bootstrap,Angular Changedetection,我有一个组件客户管理,在其中我显示客户列表,点击查看按钮,我导航到另一个组件客户详细信息,我在选项卡中显示。在客户详细信息组件上,它显示以下错误 ERROR Error: ViewDestroyedError: Attempt to use a destroyed view: detectChanges at viewDestroyedError (core.js:20452) at Object.debugUpdateDirectives [as updateDirective

我有一个组件客户管理,在其中我显示客户列表,点击查看按钮,我导航到另一个组件客户详细信息,我在选项卡中显示。在客户详细信息组件上,它显示以下错误

ERROR Error: ViewDestroyedError: Attempt to use a destroyed view: detectChanges
    at viewDestroyedError (core.js:20452)
    at Object.debugUpdateDirectives [as updateDirectives] (core.js:23908)
    at checkAndUpdateView (core.js:23307)
    at callWithDebugContext (core.js:24177)
    at Object.debugCheckAndUpdateView [as checkAndUpdateView] (core.js:23879)
    at ViewRef_.push../node_modules/@angular/core/fesm5/core.js.ViewRef_.detectChanges (core.js:21688)
    at ngx-bootstrap-dropdown.js:639
    at HTMLDocument.<anonymous> (platform-browser.js:993)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
    at Object.onInvokeTask (core.js:17290)
错误错误:ViewDestroyedError:尝试使用已销毁的视图:detectChanges
在viewDestroyedError(core.js:20452)
在Object.debugUpdateDirectives[作为updateDirectives](core.js:23908)
在checkAndUpdateView(core.js:23307)
在callWithDebugContext(core.js:24177)
在Object.debugCheckAndUpdateView[作为checkAndUpdateView](core.js:23879)
在ViewRef_uu.push../node_umodules/@angular/core/fesm5/core.js.ViewRef_uuu.detectChanges(core.js:21688)
在ngx引导下拉列表中。js:639
在HTMLDocument。(platformbrowser.js:993)
在ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask(zone.js:423)
位于Object.onInvokeTask(core.js:17290)

通常,当您订阅触发更改检测的组件内部的外部可观察对象时,会发生这种情况。当您没有取消对销毁组件的订阅时,可观察到的组件将触发,您尝试对不再存在的组件启动更改检测

典型示例:

@Component({...})
class MyComponent {
  Input() myTrigger$: Observable<any>;

  constructor(private cdr: ChangeDetectorRef) {
  }

  ngOnInit() {
    this.myTrigger$.subscribe(() => {
      // do anything useful...
      this.cdr.detectChanges(); // <- This call fails, once the component is destroyed
    });
  }
}
@组件({…})
类MyComponent{
Input()myTrigger$:可观察;
构造函数(专用cdr:ChangeDetectorRef){
}
恩戈尼尼特(){
此.myTrigger$.subscribe(()=>{
//做任何有用的事。。。

这个.cdr.detectChanges();//{/在所有这些Hussel中,当您想要更新UI时,可以使用下面的方法。这是一个更好的变更检测实现,希望在Angle文档中提到

detectChanges () {
        setTimeout(() => {
            if ( this.cdRef !== null &&
                this.cdRef !== undefined &&
                ! (this.cdRef as ViewRef).destroyed ) {
                    this.cdRef.detectChanges();
            }

        }, 250);
    }

你可以在这里或blitz上添加你的代码。很难理解当前的问题以及我所需要的。谢谢。它在没有
setTimeout
的情况下工作。谢谢,Aniket。如果你觉得有帮助,请在这里投票。谢谢。已经投票了。
detectChanges () {
        setTimeout(() => {
            if ( this.cdRef !== null &&
                this.cdRef !== undefined &&
                ! (this.cdRef as ViewRef).destroyed ) {
                    this.cdRef.detectChanges();
            }

        }, 250);
    }