【发布时间】:2017-06-14 11:04:32
【问题描述】:
在官方angular2 tutorial中包含如下代码
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 返回一个 observable,而
this.heroService.getHero(+params['id']) 返回一个承诺
这是rxjsswitchmap的签名
switchMap(project: function: Observable, resultSelector: function(outerValue, innerValue, outerIndex, innerIndex): any): Observable
我们看到第一个参数接受一个函数,该函数发出一个 observable。
但是在上面的示例中,我们实际上传入了一个发出承诺的函数。
它们彼此兼容吗?
还有控制台
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…}
是否应该将_isScalar 设置为true,因为该函数会输出一个promise?
【问题讨论】:
标签: angular promise rxjs observable