ObjectUnsubscribedError:当我在 Angx 2 中使用 ngx-progress 时出现对象取消订阅错误

2024-05-20

我在用ngx进度条 https://www.npmjs.com/package/ngx-progressbarAngular 2 应用程序中的栏。当应用程序首次加载时,它工作正常。第二次显示错误。我提到了几篇类似的文章媒体网站 https://medium.com/@fsufitch/angular-2-objectunsubscribederror-woes-abda60e04cfe用于订阅对象。我没听清楚。 每次单击路由器链接时,我都需要制作进度条。

I attached error snapshot: enter image description here

进度条码:

import { Component, AfterContentInit} from '@angular/core';
import { NgProgress } from 'ngx-progressbar'
@Component({
  selector: 'link-outlet',
  template: '<ng-progress [showSpinner]="false"></ng-progress>'
})
export class RoutingDirectiveComponent implements AfterContentInit{
  constructor(private ngProgress: NgProgress) {

  }
  ngAfterContentInit(){

   this.ngProgress.start();
   setTimeout(()=>{
     this.ngProgress.done();
   }, 2000);
  }
 }

您的建议将不胜感激。


更新 - Angular 8

这个答案在语法上对于 Angular 8 仍然有效。


我意识到这是一篇旧文章,我相信我们现在使用的是 Angular 6。但是,我收到此错误并想发布解决方案。

我打电话时遇到了这个问题.unsubscribe()直接在我调用的对象上.subscribe()在。但是,当您订阅事件时,该方法会返回一个Subscription键入的值。您需要保存它(可能在您的组件的类上),然后调用取消订阅该订阅对象当你完成时。

Examples

错误代码:

this.someEvent.subscribe(() => {
    // DO SOMETHING
})
...
this.someEvent.unsubscribe()

好的代码:

this.myEventSubscription = this.someEvent.subscribe(() => {
     // DO SOMETHING
})
...
this.myEventSubscription.unsubscribe()

同样,您需要取消订阅Subscription反对.subscribe()方法在调用时返回。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ObjectUnsubscribedError:当我在 Angx 2 中使用 ngx-progress 时出现对象取消订阅错误 的相关文章

随机推荐