最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check if an observable is complete - Stack Overflow

programmeradmin10浏览0评论

I need to show a toast after an observable is plete, how i can do it, my code is:

thismerceCtrl.UpdateCategories(this.toSave).subscribe(data => {
  }, error => {
    this.mainFunction.showError(error)
  }),
  plete => {
    this.mainFunction.showToast(this.localization.message_ok)
  }

I tried to do like this:

thismerceCtrl.UpdateCategories(this.toSave).subscribe(data => {
  }, error => {
    this.mainFunction.showError(error)
  }),
  () => {
    this.mainFunction.showToast(this.localization.message_ok)
  }

but it doesn't work

I need to show a toast after an observable is plete, how i can do it, my code is:

this.merceCtrl.UpdateCategories(this.toSave).subscribe(data => {
  }, error => {
    this.mainFunction.showError(error)
  }),
  plete => {
    this.mainFunction.showToast(this.localization.message_ok)
  }

I tried to do like this:

this.merceCtrl.UpdateCategories(this.toSave).subscribe(data => {
  }, error => {
    this.mainFunction.showError(error)
  }),
  () => {
    this.mainFunction.showToast(this.localization.message_ok)
  }

but it doesn't work

Share Improve this question edited Apr 12, 2019 at 8:45 jo_va 14k3 gold badges25 silver badges49 bronze badges asked Apr 12, 2019 at 8:29 To_MarsTo_Mars 1254 silver badges12 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

You have to pass the plete handler as the third argument to subscribe.

In your code, simply move the closing parenthesis to the end.

Change this code:

this.merceCtrl.UpdateCategories(this.toSave).subscribe(data => {
  }, error => {
    this.mainFunction.showError(error)
  }), // <===== Remove this parenthesis
  () => {
    this.mainFunction.showToast(this.localization.message_ok)
  }; // <====== It should be here

To this:

this.merceCtrl.UpdateCategories(this.toSave).subscribe(
  data => {
    // next handler body
  }, error => {
    this.mainFunction.showError(error)
  }, () => {
    this.mainFunction.showToast(this.localization.message_ok)
  }
);

You can give object parameter to subscribe() function:

this.merceCtrl.UpdateCategories(this.toSave).subscribe({
   next: value => { ... },
   error: err => { ... },
   plete: () => { ... }
});

You can try to use finally method

this.merceCtrl.UpdateCategories(this.toSave).finally(() => {
    // plete
}).subscribe((data) => {
    // success
}, (error) => {
    // error
});

Source: https://github./angular/angular/issues/7865#issuement-204408637

发布评论

评论列表(0)

  1. 暂无评论