Angular 6 - 带有子类型的 catchError
Angular 6 - catchError with subtyping
我正在开发一个 angular 6 应用程序,它可以与一些 REST 服务进行通信。所有 REST 服务的响应都包装在这样的对象中:
{
error: false,
message: '',
status: 200,
value: {}
}
值 属性 包含获取的数据,可以是任何内容。
我已将此结构重复到具有此 class
的打字稿中
export class BaseRestResponse<T> {
private _error: number;
private _message: string;
private _status: number;
private _value: T;
constructor();
constructor(error: number, message: string, status: number, value: T);
constructor(error?: number, message?: string, status?: number, value?: T) {
this._error = error;
this._message = message;
this._status = status;
this._value = value;
}
get error(): number {
return this._error;
}
get message(): string {
return this._message;
}
get status(): number {
return this._status;
}
get value(): T {
return this._value;
}
}
我有一个包含一些共享数据的基础服务和调用以处理 http 错误的函数,传递给 rxjs catchError 的函数
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { BaseRestResponse } from './base.rest.response';
const httpOptions = {
headers: new HttpHeaders({'Content-Type': 'application/json'})
};
export abstract class BaseService {
baseUrl = 'http://localhost:8080/xxx';
constructor(protected http: HttpClient) {
this.http = http;
}
/**
* @param operation
* @param result
* @returns {(error:any)=>Observable<T>}
*/
public handleHttpError<T>(operation = 'operation', result?: T) {
return (error: any): Observable<T> => {
return of(result as T);
};
}
}
然后我有一个产品服务扩展我的基本服务,如下所示:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';
import { BaseService } from './base.service';
import { Product } from './product/product';
import { BaseRestResponse } from './base.rest.response'
const LOCAL_URL = '/product/find';
@Injectable({
providedIn: 'root'
})
export class ProductService extends BaseService {
private url: string;
constructor(http: HttpClient) {
super(http);
this.url = this.baseUrl + LOCAL_URL;
}
getAll(): Observable<Product[]> {
let resp = new BaseRestResponse<Product[]>(this.http.get<Product[]>(this.url).pipe(
catchError(this.handleHttpError('getAll', BaseRestResponse<Product[]>)) // this line fails to compile
));
return resp;
}
}
但是我得到这个错误:
src/app/product.service.ts(25,76) 中的错误:错误 TS1005:'(' 预期。
src/app/product.service.ts(26,7):错误 TS1005:')' 预期。
我想做的是有一个通用的 handleHttpError 函数,可以根据需要 return BaseRestResponse,但我不知道如何解决这个问题
你的第二个论点是错误的BaseRestResponse<Product[]>
。它需要是一个值而不是对象类型
我正在开发一个 angular 6 应用程序,它可以与一些 REST 服务进行通信。所有 REST 服务的响应都包装在这样的对象中:
{
error: false,
message: '',
status: 200,
value: {}
}
值 属性 包含获取的数据,可以是任何内容。
我已将此结构重复到具有此 class
的打字稿中export class BaseRestResponse<T> {
private _error: number;
private _message: string;
private _status: number;
private _value: T;
constructor();
constructor(error: number, message: string, status: number, value: T);
constructor(error?: number, message?: string, status?: number, value?: T) {
this._error = error;
this._message = message;
this._status = status;
this._value = value;
}
get error(): number {
return this._error;
}
get message(): string {
return this._message;
}
get status(): number {
return this._status;
}
get value(): T {
return this._value;
}
}
我有一个包含一些共享数据的基础服务和调用以处理 http 错误的函数,传递给 rxjs catchError 的函数
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { BaseRestResponse } from './base.rest.response';
const httpOptions = {
headers: new HttpHeaders({'Content-Type': 'application/json'})
};
export abstract class BaseService {
baseUrl = 'http://localhost:8080/xxx';
constructor(protected http: HttpClient) {
this.http = http;
}
/**
* @param operation
* @param result
* @returns {(error:any)=>Observable<T>}
*/
public handleHttpError<T>(operation = 'operation', result?: T) {
return (error: any): Observable<T> => {
return of(result as T);
};
}
}
然后我有一个产品服务扩展我的基本服务,如下所示:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';
import { BaseService } from './base.service';
import { Product } from './product/product';
import { BaseRestResponse } from './base.rest.response'
const LOCAL_URL = '/product/find';
@Injectable({
providedIn: 'root'
})
export class ProductService extends BaseService {
private url: string;
constructor(http: HttpClient) {
super(http);
this.url = this.baseUrl + LOCAL_URL;
}
getAll(): Observable<Product[]> {
let resp = new BaseRestResponse<Product[]>(this.http.get<Product[]>(this.url).pipe(
catchError(this.handleHttpError('getAll', BaseRestResponse<Product[]>)) // this line fails to compile
));
return resp;
}
}
但是我得到这个错误:
src/app/product.service.ts(25,76) 中的错误:错误 TS1005:'(' 预期。 src/app/product.service.ts(26,7):错误 TS1005:')' 预期。
我想做的是有一个通用的 handleHttpError 函数,可以根据需要 return BaseRestResponse,但我不知道如何解决这个问题
你的第二个论点是错误的BaseRestResponse<Product[]>
。它需要是一个值而不是对象类型