![](/img/trans.png)
[英]Angular 4 - argument of type string[] is not assignable to type parameter of type 'string'
[英]Argument of type '{}' is not assignable to parameter of type in Angular 8
我有一个处理错误的方法:
private handleError<T>(operation = 'operation', result?: T) {
return (error: any): Observable<T> => {
console.error(error);
this.log(`${operation} failed: ${error.message}`);
return of(result as T);
};
}
private log(message: string) {
console.log(message);
}
我想在我的方法中使用它:
getMyStructure(): Observable<HttpResponse<StructureResponse>> {
return this.http.get< StructureResponse >(
localUrl).pipe(retry(3), catchError(this.handleError<StructureResponse>('getMyStructure', {})));
}
但最后一个{}
向我抛出了一个错误。 StructureResponse
是一个带有一些字段的接口。 根据我的理解,我应该在那里放置一些(空的?) StructureResponse
对象,但我不再确定。 你能帮我解决这个问题吗?
这是结构响应:
interface SomeStructure {
content: MyStructure[];
page: number;
size: number;
totalElements: number;
totalPages: number;
last: boolean
}
添加 ? 到每个属性。
interface SomeStructure {
content?: MyStructure[];
page?: number;
size?: number;
totalElements?: number;
totalPages?: number;
last?: boolean
}
使用Observable<StructureResponse>
代替Observable<HttpResponse<StructureResponse>>
如下。
getMyStructure(): Observable<StructureResponse> {
return this.http.get< StructureResponse >(
localUrl).pipe(retry(3), catchError(this.handleError<StructureResponse>('getMyStructure', {})));
}
如果您传递一个空对象,那么它分配给接口的唯一方法是接口中的属性都是可选的(带有?),否则您至少需要在对象中使用 null 或其他方式显示您的属性价值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.