简体   繁体   English

用于 Rxjs 中的地图运算符

[英]for map operator in Rxjs

Recently I just learned a little about rxjs and Angular.最近我刚刚了解了一些关于 rxjs 和 Angular 的知识。 And when I tried to write an api to access some web service.当我尝试编写一个 api 来访问一些 Web 服务时。

I defined this type :我定义了这种类型:

    export type Banner = {
           targetId: number;
           url: string;
           imageUrl: string;
    }

and the service part is :服务部分是:

export class HomeService {

  constructor(private http: HttpClient,
              @Inject(API_CONFIG) private uri: string) { }

  getBanners(): Observable<Banner[]> {
    return this.http.get(this.uri + 'banner')
      .pipe(map((res: { banners: Banner[] }) => res.banners));
  }
}

and tge receiving data is like this而tge接收数据是这样的

{
"banners": [
{
"imageUrl": "http://p1.music.126.net/64Nc0mcg6Sjq56TzCLDpQA==/109951167536067432.jpg",
"targetId": 0,
"adid": null,
"targetType": 3000,
"titleColor": "blue",
"url": "https://music.163.com/m/at/62a19bee9c36d0102fa275f4",
"exclusive": false,
"monitorImpress": null,
"monitorClick": null,
"monitorType": null,
"monitorImpressList": null,
"monitorClickList": null,
"monitorBlackList": null,
"extMonitor": null,
"extMonitorInfo": null,
"adSource": null,
"adLocation": null,
"adDispatchJson": null,
"encodeId": "0",
"program": null,
"event": null,
"video": null,
"song": null,
"scm": "1.music-homepage.homepage_banner_force.banner.4747964.341110454.null"
},
{
"imageUrl": "http://p1.music.126.net/MLX8OCPeIzlyxGGaHz3IbQ==/109951167532698136.jpg",
"targetId": 1954420092,
"adid": null,
"targetType": 1,
"titleColor": "red",
"url": null,
"exclusive": false,
"monitorImpress": null,
"monitorClick": null,
"monitorType": null,
"monitorImpressList": null,
"monitorClickList": null,
"monitorBlackList": null,
"extMonitor": null,
"extMonitorInfo": null,
"adSource": null,
"adLocation": null,
"adDispatchJson": null,
"encodeId": "1954420092",
"program": null,
"event": null,
"video": null,
"song": null,
"scm": "1.music-homepage.homepage_banner_force.banner.4725970.341201780.null"
}
}

When I tried to compile the code, compiler told me sth like this:当我试图编译代码时,编译器告诉我这样的:

Error: src/app/service/home.service.ts:18:13 - error TS2345: Argument of type 'OperatorFunction<{ banners: Banner[];错误:src/app/service/home.service.ts:18:13 - 错误 TS2345:'OperatorFunction<{ 横幅:横幅 [] 类型的参数; }, Banner[]>' is not assignable to parameter of type 'OperatorFunction<Object, Banner[]>'. }, Banner[]>' 不能分配给类型为 'OperatorFunction<Object, Banner[]>' 的参数。 The 'Object' type is assignable to very few other types. “对象”类型可分配给极少数其他类型。 Did you mean to use the 'any' type instead?您的意思是改用“任何”类型吗? Property 'banners' is missing in type 'Object' but required in type '{ banners: Banner[]; “对象”类型中缺少属性“横幅”,但类型“{横幅:横幅[]; }'. }'。

Typescript version: 4.5.2打字稿版本:4.5.2

Angular version: 13.1.0角度版本:13.1.0

Try to specify the response type of the HTTP client as well:尝试指定 HTTP 客户端的响应类型:

return this.http.get<{ banners: Banner[] }>(this.uri + 'banner')
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM