【发布时间】:2020-03-19 04:20:57
【问题描述】:
我在下面上课:
export class RestService {
private baseUrl: string;
constructor(protected http: HttpClient) {
this.baseUrl = environment.LOCAL_URL;
}
public get<T>(resource: string, params?: HttpParams): Observable<T> {
const url = this.PrepareUrl(resource);
return this.http.get<T>(url, { params }).pipe(
retry(2),
catchError(this.catchBadResponse)
);
}
public post<T>(resource: string, model: any): Observable<T> {
const url = this.PrepareUrl(resource);
const headers = new HttpHeaders({ 'Content-Type': 'application/json' });
return this.http.post<T>(url, model, { headers }).pipe(
retry(2),
catchError(this.catchBadResponse)
);
}
public put<T>(resource: string, model: any): Observable<T> {
const url = this.PrepareUrl(resource);
return this.http.put<T>(url, model).pipe(
retry(2),
catchError(this.catchBadResponse)
);
}
public delete(resource: string, id: any): Observable<any> {
const url = this.PrepareUrl(resource) + `\\${id}`;
return this.http.delete(url).pipe(
retry(2),
catchError(this.catchBadResponse)
);
}
protected PrepareUrl(resource: string): string {
return `${this.baseUrl}/${resource}`;
}
protected catchBadResponse(error: HttpErrorResponse) {
console.log('error occured!');
return throwError(error);
}
}
以及另一个扩展 RestService 类的类:
export class PersonRestService extends RestService {
constructor(protected http: HttpClient) {
super(http);
}
public get<T>(params?: HttpParams): Observable<T> {
return super.get<T>('person', params);
}
public post<T>(model: any): Observable<T> {
return super.post('person', model);
}
}
我想覆盖子类中的一些函数,但我从 ide 得到了这个提示(错误):
“PersonRestService”类型中的属性“get”不可分配给 基本类型“RestService”中的相同属性。输入'(参数?: HttpParams) => Observable' 不可分配给类型 '(resource: 字符串,参数?:HttpParams) => Observable'。 参数“params”和“resource”的类型不兼容。 类型 'string' 不可分配给类型 'HttpParams'.ts(2416)
我该怎么办?
【问题讨论】:
标签: angular typescript oop inheritance