大佬教程收集整理的这篇文章主要介绍了angular – 可观察并承诺在rxjs中兼容吗?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
getHeroes(): Promise<Hero[]> { return Promise.resolve(HEROES); } ngOnInit(): void { this.route.params .switchMap((params: Params) => this.heroservice.getHero(+params['id'])) .subscribe(hero => this.hero = hero); console.log(this.route.params); console.log(this.route.params .switchMap((params: Params) => this.heroservice.getHero(+params['id']))); }
现在我们知道this.route.params返回一个可观察的,而
this.heroservice.getHero(params [‘id’])返回一个promise
这是rxjs switchmap的签名
switchMap(project: function: Observable,resultSELEctor: function(outerValue,innerValue,outerIndex,innerIndeX): any): Observable
但是在上面的例子中,我们实际上传递了一个发出promise的函数.
它们是否相互兼容?
此外还有控制台
console.log(this.route.params .switchMap((params: Params) => this.heroservice.getHero(+params['id'])));
AnonymousSubject {_isScalar: false,observers: Array[0],closed: false,isStopped: false,hasError: false…}
ObservableInput
:
export type ObservableInput<T> = SubscribabLeorPromise<T> | ArrayLike<T>;
这可以是一个可观察的,一个promise或一个类似数组的可迭代对象.
在您的问题中包含的代码中,传递给switchMap
的项目函数返回一个promise.没关系,因为SwitchMap接受返回ObservableInput的项目函数:
export function switchMap<T,R>( this: Observable<T>,project: (value: T,index: number) => ObservableInput<R> ): Observable<R>;
switchMap实现将看到promise被转换为observable.
关于结果observable的内部_isScalar属性,当promise解析并且解析的值存储在observable中时,它将被设置为true.我的理解是,在RxJS中,标量不是指将要发射的值的数量,而是指所述值是否可用于立即发射.
以上是大佬教程为你收集整理的angular – 可观察并承诺在rxjs中兼容吗?全部内容,希望文章能够帮你解决angular – 可观察并承诺在rxjs中兼容吗?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。