【发布时间】:2021-08-01 00:14:46
【问题描述】:
有没有更好的方法来重写此代码并避免链接订阅?
我为什么要链接?因为我需要在子订阅中输出source1$
而且我还有if 条件,因为我想有条件地调用子订阅
PS 我在post 中检查了解决方案
这是stackblitz link 和代码
import { from } from 'rxjs';
//emit array as a sequence of values
const source1$ = from([1]);
const source2$ = from([2]);
const source3$ = from([3]);
const useCond1 = true; // this is dynamic can be false too
const useCond2 = true; // this is dynamic can be false too
source1$.subscribe(val => {
if (useCond1) {
source2$.subscribe(() => {
console.log('val from source1 in source2', val);
});
}
if (useCond2) {
source3$.subscribe(() => {
console.log('val from source1 in source3', val);
});
}
});
【问题讨论】:
标签: rxjs