gpt4 book ai didi

angular - 我做错了什么 RxJS 运营商

转载 作者:可可西里 更新时间:2023-11-01 16:31:46 28 4
gpt4 key购买 nike

我正在学习 Angular 和 RxJS 运算符。我正在使用 Hearthstone API 来训练它。我究竟做错了什么 ?我想过滤流以仅返回“萨满”卡。我知道,Api 让我有机会按类(class)获得卡片,但我只想训练 RxJS,所以我正在考虑这个问题。我的 ts 组件:

cards: Card[];

ngOnInit() {
this.cardsService.getCards().pipe(
filter((el: any) => el.playerClass === 'Shaman')
).subscribe(res =>
this.cards = res);
}

这是我的服务:

export class CardsService {

constructor(private http: HttpClient) {
}

getCards(): Observable<Card[]> {
const headers = new HttpHeaders().set('X-RapidAPI-Key', '78976c5326mshb1a81046ad68d52p11b362jsn35ec089dd9db');
return this.http.get<Card[]>('https://omgvamp-hearthstone-v1.p.rapidapi.com/cards/sets/Basic', { headers });
}
}

最佳答案

如果您的可观察流返回类型是一个数组,那么流中的项目就是数组。过滤器管道运算符将过滤流中的项目。您的问题来自将任何类型放在过滤器输入上 - 删除并且它应该是 card[] 类型。然后您尝试从数组中提取一个属性。

关于angular - 我做错了什么 RxJS 运营商,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54776308/

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