“ObjectUnsubscribedError” - 使用get call

时间:2018-06-11 09:28:43

标签: angular rxjs angular5 observable subscribe

this.Showdata = false;
this.showSpinner = true;
this.subscription = this.assessmentlistService.getAll().subscribe(response => {
        if (response != null) {
            if (response.StatusCode >= 200 && response.StatusCode < 300) {
                this.AssessmentList = response.Data;
                this.Showdata = true;
            }
            else {
                //this.AssessmentList = [];
            }
            this.updateAssessmentList.next();
        }
        this.showSpinner = false;
    },
    error => {
        this.messageData = new MessageData();
        this.messageData.Message = "Server Error, please try again later";
        this.messageData.ShowErrorMessage = true;
        this.showSpinner = false;
    }
);

这是我的api调用方法。我尝试了取消订阅方法。它不起作用。我用以下代码每1分钟刷新数据表。

Refresh() {
    setInterval(() => {
        this.subscription.unsubscribe();
        this.Showdata = false;
        this.showSpinner = true;
        this.subscription = this.assessmentlistService.getAll().subscribe(response => {
                if (response != null) {
                    if (response.StatusCode >= 200 && response.StatusCode < 300) {
                        this.AssessmentList = response.Data;
                        this.Showdata = true;
                    }
                    else {
                        //this.AssessmentList = [];
                    }
                    this.updateAssessmentList.next();
                }
                this.showSpinner = false;
            },
            error => {
                this.messageData = new MessageData();
                this.messageData.Message = "Server Error, please try again later";
                this.messageData.ShowErrorMessage = true;
                this.showSpinner = false;
            }
        );
    }, 60000);
}

Iam尝试重新加载数据表时出现此错误。

  

ObjectUnsubscribed Error“{name:”ObjectUnsubscribedError“,stack:”ObjectUnsubscribedError:objectunsubscribed↵“... ... ModuleList = ScorebinComponent.ngfactory.js:549:5)”,message:“object unsubscribed”,ngDebugContext:DebugContext_,ngErrorLogger:ƒ}   信息   :   “对象取消订阅”   名称   :   “ObjectUnsubscribedError”   ngDebugContext   :   DebugContext_ {view:{...},nodeIndex:74,nodeDef:{...},elDef:{...},elView:{...}}   ngErrorLogger   :   ƒ()   堆   :   “ObjectUnsubscribed:错误的对象unsubscribed”(webpack-internal:///./node_modules/rxjs/_esm5/util/ObjectUnsubscribedError.js:22:26)↵在Subject._trySubscribe(webpack-internal:/// ./ node_modules / rxjs / _esm5 / Subject.js:105:19)↵在Subject.Observable.subscribe(webpack-internal:///。/ node_modules / rxjs / _esm5 / Observable.js:166:93)↵在DataTableDirective.ngOnInit (webpack-internal:///./node_modules/angular-datatables/src/angular-datatables.directive.js:23:28)↵在checkAndUpdateDirectiveInline(webpack-internal:///./node_modules/@angular/core/ esm5 / core.js:12627:19)↵在checkAndUpdateNodeInline(webpack-internal:///./node_modules/@angular/core/esm5/core.js:14151:20)↵at checkAndUpdateNode(webpack-internal:// /./node_modules/@angular/core/esm5/core.js:14094:16)↵在debugCheckAndUpdateNode(webpack-internal:///./node_modules/@angular/core/esm5/core.js:14987:76) ↵在debugCheckDirectivesFn(webpack-internal:///./node_module s /@angular/core/esm5/core.js:14928:13)↵在Object.eval [as updateDirectives](ng:///AssessmentModule/AssessmentListComponent.ngfactory.js:549:5)“   的   :   错误

1 个答案:

答案 0 :(得分:0)

如果您想每1分钟重复一次代码,请使用Observable的{​​{1}}运算符更方便,更清晰:

interval

Observable .interval(60*1000) //one minute .flatMap(() => this.assessmentlistService.getAll()) .subscribe(response => { if (response != null) { //.... your other code } }); 允许您在每次解决后重复调用.flatMap() API(想想重复订阅)。