我正在尝试实现一些缓存,并且我有以下方法。该catchError
块导致错误,因为返回类型变为Observable<Client | Client[]>
. 我不明白为什么它认为它不是一组客户端,或者如何修复。
#clients: Client[] = []
#clientsLastModified: string | null = null
index(): Observable<Client[]> {
let headers = new HttpHeaders()
if (this.#clientsLastModified)
headers = headers.set('If-Modified-Since', this.#clientsLastModified)
return this.http.get<IClientDTO[]>('clients', {headers, observe: 'response'}).pipe(
map(x => {
this.#clientsLastModified = x.headers.get('Last-Modified')
if (x.status === HttpStatusCode.Ok && x.body) {
this.#clients = x.body.map(x => new Client(x))
return this.#clients
} else
return of([])
}),
catchError((err: unknown) => {
if (err instanceof HttpErrorResponse && err.status === HttpStatusCode.NotModified)
return this.#clients
return throwError(() => err)
})
)
}