【问题标题】:Why does the origin observable emits values, if an observable returned by an operator gets subscribed?如果操作员返回的可观察对象被订阅,为什么原始可观察对象会发出值?
【发布时间】:2018-04-09 05:39:33
【问题描述】:

需要订阅冷的 observable 才能发出值。 当使用 map 等操作符时,会返回一个新的 observable。

const origin$ = Rx.Observable.from([1,2,3,4]);
const mapped$ = origin$.map(val => val+1);
mapped$.subscribe(console.log);

使用.subscribemapped$ observable,值会发出并记录在控制台中。

但在这种情况下,mapped$ observable 被订阅了,而不是 origin$ observable。为什么origin$ observable 开始发出值?

【问题讨论】:

标签: angular rxjs observable subscribe reactivex


【解决方案1】:

使用任何运算符(老式的 .map() 或现在的 .pipe(map()))不会返回订阅。相反,它返回一个 new observable,它将另一个 observable 作为源,但等待任何订阅,直到它有订阅。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    • 2020-12-10
    • 2018-04-11
    • 2018-12-16
    • 1970-01-01
    • 2022-10-17
    相关资源
    最近更新 更多