我收到一個錯誤:
Type 'Observable<void | PostProps[]>' is not assignable to type 'Observable<PostProps[]>'. Type 'void | PostProps[]' is not assignable to type 'PostProps[]'. Type 'void' is not assignable to type 'PostProps[]'.ts(2322)
但我正在回傳回應。但怎么map不明白?
這是錯誤部分:
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, throwError } from 'rxjs';
import { catchError, map, shareReplay } from 'rxjs/operators';
export interface PostProps {
userId: number;
id: number;
title: string;
body: string;
processed: string;
}
@Injectable()
export class PersonnelDataService {
list$: Observable<PostProps[]>;
private URL = 'https://jsonplaceholder.typicode.com/posts';
constructor(private http: HttpClient) {}
fetchPersonnelList() {
if (!this.list$) {
this.list$ = this.http.get<PostProps[]>(this.URL).pipe( //error
map((response: PostProps[]) => response),
shareReplay(1),
catchError(async (error) => this.handleError(error))
);
}
return this.list$;
}
handleError(error) {
throwError(error);
}
}
uj5u.com熱心網友回復:
你得到一個void型別,因為catchError它不回傳任何東西。catchError所以在state中回傳一個空陣列。
catchError((error) => {
this.handleError(error);
return [];
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/522003.html
標籤:有角度的打字稿打字稿打字
