为什么我们应该使用 RxJs 的 of() 函数?
- 论坛
- 为什么我们应该使用 RxJs 的 of() 函数?
25 浏览
为什么我们应该使用 RxJs 的 of() 函数?
在angular.io的Angular2教程的服务部分中,我遇到了一个名为of的方法。例如:
getHeroes(): Observable{ return of(HEROES); }
或者在下面的示例中:
getHero(id: number): Observable{ // Todo: 在获取英雄之后发送消息 this.messageService.add(`HeroService: fetched hero id=${id}`); return of(HEROES.find(hero => hero.id === id)); }
在angular.io中只是解释了:
使用RxJS的of()方法返回一个模拟英雄的Observable(Observable
)。
但没有解释为什么我们应该使用of操作符,它具体做什么以及它的好处是什么?