打字稿继承中的覆盖功能

时间:2019-11-23 07:02:05

标签: angular typescript oop inheritance

我在课上吼叫:

  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”中的相同属性。输入'(params ?:   HttpParams)=> Observable'不能分配给类型'(resource:   字符串,参数?:HttpParams)=>可观察'。       参数“ params”和“ resource”的类型不兼容。         无法将“字符串”类型分配给“ HttpParams”类型。ts(2416)

我该怎么办?

2 个答案:

答案 0 :(得分:1)

好像您遇到了following错误。

目前,您可以执行以下两项操作之一:

  1. 更改您的签名以使其与100%匹配

    public get(资源:字符串,参数?:HttpParams):可观察的{     返回super.get('person',params);   }

或者为了使其更好一点,请更改顺序并将其设置为可选:

public get<T>(params?: HttpParams, resource: string = ''): Observable<T> {
    return super.get<T>(params,'person');  
  }
  1. PersonRestService类中删除泛型。

第二个对我来说更有意义。您知道您的资源是人,因此您可以这样:

  public getPerson(params?: HttpParams): Observable<object> {
    return super.get<object>(params,'person');  
  }

答案 1 :(得分:1)

在打字稿中,我们不能100%覆盖方法;就像这个问题,我们不能覆盖旧方法。 有一句qoute说:“喜好构成是过继承的”; 所以我像下面这样更改代码片段:

1-请勿更改RestService:

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);
  }
}

2-删除在PersonRestService中以RestService形式扩展并在构造函数中注入RestService:

export class PersonRestService {

  constructor(private restService: RestService) {
  }
  public get<T>(params?: HttpParams): Observable<T> {
    return this.restService.get<T>('person', params);
  }
}

完成! 现在我可以玩代码了。