Ecmascript 6 多播可观察:尝试订阅“中的结果”;无法读取属性';订阅';“未定义”的定义;错误
我的代码中需要执行一个AJAX请求并将结果数据发送到两个不同的地方,因此我认为使用多播可观测是实现这一点的最简单方法。我的代码如下所示: 在我的“应用程序”对象的构造函数中:Ecmascript 6 多播可观察:尝试订阅“中的结果”;无法读取属性';订阅';“未定义”的定义;错误,ecmascript-6,rxjs,rxjs5,Ecmascript 6,Rxjs,Rxjs5,我的代码中需要执行一个AJAX请求并将结果数据发送到两个不同的地方,因此我认为使用多播可观测是实现这一点的最简单方法。我的代码如下所示: 在我的“应用程序”对象的构造函数中: this.getEpisodeDescription = (id) => jsonLoader("http://www.randomtext.me/api/lorem/p-2/8-24", "text_out"); function jsonLoader (url, field) { let stream
this.getEpisodeDescription = (id) => jsonLoader("http://www.randomtext.me/api/lorem/p-2/8-24", "text_out");
function jsonLoader (url, field)
{
let stream = Rx.Observable.ajax ({ url: url, crossDomain: true })
.retry (1)
.pluck ("response");
if (field !== undefined)
return stream.pluck(field);
else
return stream;
}
我以前成功地使用过这个方法来检索单个接收器的数据,所以我确信它可以正常工作。但是,调用方是新的:
loadSummary (id)
{
let cachedValue = this.summaries.get(id);
if (cachedValue !== undefined) return Rx.Observable.of(cachedValue);
let observable = this.app.getEpisodeDescription(id);
let multicast = observable.multicast ().refCount ();
multicast.subscribe(result => this.summaries.put(id, result));
return multicast;
}
当我尝试执行此方法时,我得到以下堆栈跟踪:
Uncaught TypeError: Cannot read property 'subscribe' of undefined
at Observable.ConnectableObservable._subscribe (app.js:44193)
at Observable._trySubscribe (app.js:10253)
at Observable.subscribe (app.js:10241)
at RefCountOperator.call (app.js:44275)
at Observable.subscribe (app.js:10238)
at AsyncAction.SubscribeOnObservable.dispatch (app.js:71532)
at AsyncAction._execute (app.js:21083)
at AsyncAction.execute (app.js:21058)
at AsyncScheduler.flush (app.js:21156)
(忽略文件名和行号——我使用的是webpack,目前它似乎没有生成工作行号图)
知道发生了什么事吗?具体地说,我是如何从调用
多播
中获得一个对象的,该对象具有适当的订阅
等方法,但当您尝试订阅它时,它显然无法订阅父对象的?多播()的第一个参数运算符是主题工厂函数或主题实例
这意味着,如果您希望有一个共享主题实例,则应该像这样使用它:
let multicast = observable.multicast(new Subject()).refCount();
。。。或者像这样为每个观察者创造一个新的主题:
let multicast = observable.multicast(() => new Subject()).refCount();