gpt4 book ai didi

javascript - Rxjs - switchMap 到 http 将可观察到的字符从热变为冷

转载 作者:行者123 更新时间:2023-11-30 09:43:02 24 4
gpt4 key购买 nike

有人能解释一下为什么下面的代码会三次触发对服务器的请求吗?如果我直接订阅 http.get() 我知道它是冷可观察的,所以它会调用服务器 3 次,我需要使用 .share() 来避免这种情况。但是为什么当我订阅热门主题时会有同样的行为......很奇怪:

let testTrigger = new Subject<any>();


let testTask$ = testTrigger.switchMap(()=> this.restClient.get('onet'));

testTask$.subscribe(console.log.call(console));
testTask$.subscribe(console.log.call(console));
testTask$.subscribe(console.log.call(console));
testTrigger.next(1);

最佳答案

事实上,大多数运营商都会这样做。即如果 obs 是热的,那么 obs.op 通常是冷的。一些运算符还返回热可观察值(例如 groupBy)。最后,您需要阅读文档或进行测试,以了解您手中的可观察对象的性质。

更多详情可以查看Hot and Cold observables : are there 'hot' and 'cold' operators? .

关于javascript - Rxjs - switchMap 到 http 将可观察到的字符从热变为冷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40185925/

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