gpt4 book ai didi

angular - 使用 Response -> HttpResponse 将项目从 Angular 5 问题迁移到 8 问题

转载 作者:搜寻专家 更新时间:2023-10-30 21:25:16 25 4
gpt4 key购买 nike

显然您需要在迁移之前手动更改 Http作为@angular/http消失有利于@angular/common/http所有的类都有一点变化。

public getData<T>(pathMock: string, pathApi: string): Observable<T> {
let base: string = (this.getMockIdx()) ? `${AppGlobal.BASE_URL}` : `${AppGlobal.API_URL}`;
let path: string = (this.getMockIdx()) ? `${pathMock}` : pathApi;
let url: string = `${base}${path}`;
return this.http.get(
url
).map((response: HttpResponse<any>) => {
let result: T = response;
return result;
});
}

这就是我改变的ResponseHttpResponse<any> (我放任何因为我只是想让它工作但我不知道放什么)。

这会触发以下错误消息:

is not assignable to the constraint of type 'T'

“...不可分配给‘T’类型的约束”

但是如果我改变 T 它会变得更加困惑:

enter image description here

回答后更新:

return this.http.get<any>(`${AppGlobal.API_URL}my/api/auth/refresh/tokens`,
this.getRequestOptions()).pipe(
map((data: any) => {
return this.decryptToken(data);
}
));

with second passed argument there is also a error

第二个传递的参数也有一个错误:“ header 类型的参数: header 不可分配给 ...

这是怎么回事?

最佳答案

实际上你用HttpClient得到的不是HttpResponse,但是HttpClient已经解析了你的响应,所以你得到的是一个匿名对象。我看到您正在使用 any。不惜一切代价避免这种情况,键入您的数据,让您的生活更轻松。但在这里,我将使用 any,因为您还没有输入数据。也像在其他答案中一样,如果您还没有使用可管道运算符,请更新 rxjs。此外,我没有看到实际使用 map 有任何用处,因为您没有对 map 中的数据做任何事情,但如果您这样做,它应该看起来像:

import { map } from 'rxjs/operators';

// ...

// replace 'any' with your type, for example an interface
return this.http.get<any>('url').pipe(
map((data: any) => {
return data;
}
)

关于angular - 使用 Response -> HttpResponse 将项目从 Angular 5 问题迁移到 8 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57902634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com