[英]Getting the value from a return observable inside catchError
budgetTestService.getBudgetDates
返回一個 observable,如何在BudgetDate[]
從中獲取BudgetDate[]
值?
this.budgetService.getBudgetDays(this.startDate, this.finishDate)
.pipe(
catchError(error => {
self.timelineBudgetDates = self.budgetTestService.getBudgetDates(self.startDate, self.finishDate);
return of('some return value');
}
)
)
.subscribe(res => {
self.timelineBudgetDates = self.processDates(res);
});
錯誤:
Type 'Observable<BudgetDate[]>' is missing the following properties from type 'BudgetDate[]': length, pop, push, concat, and 25 more.
您可以使用高階函數將此引用傳遞給 catch
const errorHandler=(self)=>error => {
self.timelineBudgetDates =
self.budgetTestService.getBudgetDates(self.startDate, self.finishDate);
return of('some return value');
}
)
this.budgetService.getBudgetDays(this.startDate, this.finishDate)
.pipe(
catchError(errorHandle(this))
)
.subscribe(res => {
self.timelineBudgetDates = self.processDates(res);
});
如果您從組件調用 budgetService,您應該能夠在無需訂閱的情況下映射響應。 catchError 僅在服務拋出錯誤時執行。 請看下面的代碼...
this.budgetService.getBudgetDays(this.startDate, this.finishDate)
.pipe(
map(res => { self.timelineBudgetDates = self.processDates(res); }),
catchError(error => {
// handle error
})
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.