为什么不在RxJS中调用所有订户

时间:2017-02-17 05:35:36

标签: javascript rxjs

这是我的代码

let x = Rx.Observable.create(obs => window.obs = obs);
y = x.map(x=>x+2);
x.subscribe(v=>console.log("x got ",v));
y.subscribe(v=>console.log("y got ",v));
obs.next(1);

输出

y got 3

我期待输出

x got 1
y got 3

我在这里缺少什么?感谢

1 个答案:

答案 0 :(得分:2)

您在第二次订阅时覆盖var greeting = function(name) { console.log("Great to see you," + " " + name); }; greeting("Matt");

如果您的用例要求您在程序上调用排放,那么您可以使用主题而不是自己创建一个观察点:

window.obs
const subj = new Rx.Subject();
const sub1 = subj.subscribe(x => console.log('x got: ' + x));
const sub2 = subj.map(v => v * 2).subscribe(y => console.log('y got: ' + y));
subj.next(2);