Angular typescript继承中的覆盖函数
我有一门课叫:Angular typescript继承中的覆盖函数,angular,typescript,oop,inheritance,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
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{
私有baseUrl:string;
构造函数(受保护的http:HttpClient){
this.baseUrl=environment.LOCAL\u URL;
}
公共get(资源:string,params?:HttpParams):可观察{
const url=this.PrepareUrl(资源);
返回这个.http.get(url,{params}).pipe(
重试(2),
catchError(此.catchBadResponse)
);
}
public post(资源:string,模型:any):可观察{
const url=this.PrepareUrl(资源);
const headers=new-HttpHeaders({'Content-Type':'application/json'});
返回这个.http.post(url,模型,{headers}).pipe(
重试(2),
catchError(此.catchBadResponse)
);
}
公共put(资源:string,模型:any):可观察{
const url=this.PrepareUrl(资源);
返回这个.http.put(url,model).pipe(
重试(2),
catchError(此.catchBadResponse)
);
}
公共删除(资源:string,id:any):可观察{
constURL=this.PrepareUrl(资源)+`\\\${id}`;
返回此.http.delete(url).pipe(
重试(2),
catchError(此.catchBadResponse)
);
}
受保护的PrepareUrl(资源:string):string{
返回`${this.baseUrl}/${resource}`;
}
受保护的catchBadResponse(错误:HttpErrorResponse){
console.log('发生错误!');
返回投掷器(错误);
}
}
以及另一个扩展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);
}
}
导出类PersonRestService扩展RestService{
构造函数(受保护的http:HttpClient){
超级(http);
}
公共get(params?:HttpParams):可观察{
返回super.get('person',params);
}
公共岗位(型号:任何):可观察{
返回super.post('person',model);
}
}
我想重写子类中的某些函数,但ide有以下提示(错误):
类型“PersonRestService”中的属性“get”不可分配给
基类型“RestService”中的相同属性。类型“(参数):
HttpParams)=>Observable“不可分配给类型”(资源:
字符串,params?:HttpParams)=>可观察的'。
参数“params”和“resource”的类型不兼容。
类型“string”不可分配给类型“HttpParams”.ts(2416)
我该怎么办?你好像碰到了问题
现在,您可以做以下两件事之一:
public get<T>(params?: HttpParams, resource: string = ''): Observable<T> {
return super.get<T>(params,'person');
}
在typescript中,我们不能100%覆盖方法;就像这个问题一样,我们不能覆盖遗留方法。 有一句古特的话:“偏爱构图胜过继承”; 因此,我对代码片段进行如下更改: 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);
}
}
导出类RestService{
私有baseUrl:string;
构造函数(受保护的http:HttpClient){
this.baseUrl=environment.LOCAL\u URL;
}
公共get(资源:string,params?:HttpParams):可观察{
const url=this.PrepareUrl(资源);
返回这个.http.get(url,{params}).pipe(
重试(2),
catchError(此.catchBadResponse)
);
}
public post(资源:string,模型:any):可观察{
const url=this.PrepareUrl(资源);
const headers=new-HttpHeaders({'Content-Type':'application/json'});
返回这个.http.post(url,模型,{headers}).pipe(
重试(2),
catchError(此.catchBadResponse)
);
}
公共put(资源:string,模型:any):可观察{
const url=this.PrepareUrl(资源);
返回这个.http.put(url,model).pipe(
重试(2),
catchError(此.catchBadResponse)
);
}
公共删除(资源:string,id:any):可观察{
constURL=this.PrepareUrl(资源)+`\\\${id}`;
返回此.http.delete(url).pipe(
重试(2),
catchError(此.catchBadResponse)
);
}
受保护的PrepareUrl(资源:string):string{
返回`${this.baseUrl}/${resource}`;
}
受保护的catchBadResponse(错误:HttpErrorResponse){
console.log('发生错误!');
返回投掷器(错误);
}
}
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);
}
}
导出类PersonRestService{
构造函数(私有restService:restService){
}
公共get(params?:HttpParams):可观察{
返回此.restService.get('person',params);
}
}
完成了!
现在我可以玩代码了
export class PersonRestService {
constructor(private restService: RestService) {
}
public get<T>(params?: HttpParams): Observable<T> {
return this.restService.get<T>('person', params);
}
}