转换Angular2 HTTP响应。ConnectableObservable [英] Converting Angular2 Http response to ConnectableObservable

查看:735
本文介绍了转换Angular2 HTTP响应。ConnectableObservable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须承认,我做我的第一个步骤,Angular2,和我遇到了一个在这里的问题,我有一些问题的认识。我使用angular2@2.0.0-beta.0,这对rxjs@5.0.0-beta.0的依赖。

I must admit that I am doing my first steps with Angular2, and I am running into an issue here, which I have some problems understanding. I am using angular2@2.0.0-beta.0, which has a dependency on rxjs@5.0.0-beta.0.

我的目的是使一个HTTP请求(到REST服务),并且允许响应于被发送到返回的可观察的多个订户。如果我理解正确的文件,我可以使用发布()函数来观测通过例如转换返回在http.post功能到ConnectableObservable,通过调用ConnectableObservable.subcribe(...)多次注册多个subsribers,然后调用ConnectableObservable.connect()实际执行HTTP请求,例如像这样的:

My intention is to make an HTTP request (to a REST service) and allow the response to be sent to multiple subscribers of the returned observable. If I understand the documentation correctly, I can use the publish() function to convert the Observable returned by e.g. the http.post function to a ConnectableObservable, register multiple subsribers by invoking ConnectableObservable.subcribe(...) multiple times and then invoke ConnectableObservable.connect() to actually perform the HTTP request, e.g. like this:

var obs: Observable<Response> = this.http.post(...);
var cobs: ConnectableObservable<Response> = obs.publish();
cobs.subscribe(sub1);
cobs.subscribe(sub2);
cobs.connect();

至少我的IDE缝同意这种说法,并且不显示任何警告。运行code,我却收到以下错误:

At least my IDE seam to agree with this and does not show any warnings. Running the code, I do however get the following error:

例外的是:评估过程中的错误,点击搜索
  最初的异常:类型错误:obs.publish不是一个函数

EXCEPTION: Error during evaluation of "click"
ORIGINAL EXCEPTION: TypeError: obs.publish is not a function

如果我检查 OBS 对象在调试器中,只有的记录功能实际可用。如果我期待到实施观测类的,只有少数人的记录功能能够实现的。大部分的功能​​,其中包括发布功能,只是声明,没有任何实际执行的函数签名。

If I examine the obs object in the debugger, only a very small subset of the documented functions are actually available. If I look into the implementation of the Observable class, only a few of the documented functions are indeed implemented. Most of the functions, among them the publish function, are only declared as a function signature without any actual implementation.

我做得显然是错误的,或在这里我已完全误解了如何使用RxJS观测工作的?

Am I doing something obviously wrong here or have I misunderstood completely how to work with RxJS observables?

如果它很重要,我有一口建设,使用NPM解决和下载依赖关系和包括我node_modules目录rxjs /包/ Rx.js。

If it matters, I am building with gulp, using npm to resolve and download dependencies and including rxjs/bundles/Rx.js from my node_modules directory.

推荐答案

其实,我觉得用一个 ConnectableObservable 是没有必要的。这里是我做了测试,并收到响应当两个用户分别称为:

In fact, I think that using a ConnectableObservable isn't necessary. Here is the test I made and both subscribers are called when the response is received:

var observable =
  this.http.get('https://angular2.apispark.net/v1/companies/')
           .map(res => res.json());

observable.subscribe(
  data => console.log('subscribe #1'));
observable.subscribe(
  data => console.log('subscribe #2'));

修改

我觉得共享操作员可满足您的需要:

I think that the share operator can fit your needs:

var observable =
  this.http.get('https://angular2.apispark.net/v1/companies/')
           .map(res => res.json()).share();

observable.subscribe(
  data => console.log('subscribe #1'));
observable.subscribe(
  data => console.log('subscribe #2'));

它允许创建一个可观察到的连接(即共享方法返回一个热点观察到)。在这种情况下,只执行一个HTTP请求...

It allows to create a connectable observable (the share method returns an hot observable). In this case, only one HTTP request is executed...

这个问题可以对你有所帮助:<一href=\"http://stackoverflow.com/questions/34018252/hot-and-shared-observable-from-an-eventemitter\">Hot和共享来自EventEmitter 观察到。

This question could be helpful for you: Hot and shared Observable from an EventEmitter.

EDIT1

在评论一些讨论后,看来这个问题是关于为什么会出现错误follwing:类型错误:obs.share不是一个函数,为什么几乎所有的在观察到不可用的记录功能,通过后函数返回。

After some discussions in comments, it appears that the question was about why the follwing error occurs: TypeError: obs.share is not a function and why are almost all of the documented functions not available in the observable returned by the post function.

所以,解决办法是显式导入RxJS运营商,使他们在运行时可用。

So the solution was to explicitly import RxJS operators to make them available at runtime.

有两种解决方案。每个操作员导入:

There are two solutions. Importing per operator:

import 'rxjs/add/operator/map'

或更普遍的这个,如果你想为观测所有可用的运营方式:

Or more generally this if you want to have all available operator methods for observables:

import 'rxjs/Rx';

希望它可以帮助你,
蒂埃里

Hope it helps you, Thierry

这篇关于转换Angular2 HTTP响应。ConnectableObservable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆