forkJoin 不在 SwitchMap/MergeMap in Epics 时的行为是什么
What Is the behavior of forkJoin when it is not In a SwitchMap/MergeMap in Epics
我想了解当我直接通过管道传递给 action$ 并尝试使用 forkJoin 运算符时会发生什么
const action1 = { type: "ACTION_1" };
const action2 = { type: "ACTION_2" };
在 switchMap forkJoin 中工作正常。
export const testForkJoinSwitchMap: Epic<Action> = action$ =>
action$.pipe(
ofType(action1),
switchMap(() =>
forkJoin(
from(fetch("https://api.github.com/users")).pipe(
map((res: any) => {
return res;
})
)
)
),
map((data: any) => {
// do something with data
return action2;
})
);
如果我从 switchMap 中取出它,那么:
export const testForkJoin: Epic<Action> = action$ =>
action$.pipe(
ofType(action1),
forkJoin(from(fetch("https://api.github.com/users"))).pipe(
map((response: any) => {
return action2;
})
)
);
我收到输入错误:
Argument of type 'Observable<{ type: string; }>' is not assignable to parameter of type 'OperatorFunction<{}, Action<any>>'.
我想知道为什么编译不通过?以及类型不匹配的原因,在这种情况下,是什么导致没有 forkJoin 的 epics 无效?
编辑:我知道 forkJoin 对于单个 observable 没有意义,但我放 1 是为了让示例更小
observable.pipe()
仅在其中包含 operators。
forkJoin 是一个 returns 一个 observable 的运算符,这就是你得到那个错误的原因。 Argument of type 'Observable' is not assignable to parameter of type 'OperatorFunction'
即 forkJoin,其中 returns 一个可观察对象不能分配给运算符函数的类型。
另一方面,switchMap 是一个运算符,它 returns 一个 OperatorFunction 并在一个可观察对象上运行,这就是您的第一种方法起作用的原因。 switchMap(() => someObservable)
和 someObservable
在这种情况下是 forkJoin()
从您的导入中也可以看出这一点。您可能从 rxjs
库导入了 forkJoin,而从 rxjs/operators
库导入了 switchMap。
我想了解当我直接通过管道传递给 action$ 并尝试使用 forkJoin 运算符时会发生什么
const action1 = { type: "ACTION_1" };
const action2 = { type: "ACTION_2" };
在 switchMap forkJoin 中工作正常。
export const testForkJoinSwitchMap: Epic<Action> = action$ =>
action$.pipe(
ofType(action1),
switchMap(() =>
forkJoin(
from(fetch("https://api.github.com/users")).pipe(
map((res: any) => {
return res;
})
)
)
),
map((data: any) => {
// do something with data
return action2;
})
);
如果我从 switchMap 中取出它,那么:
export const testForkJoin: Epic<Action> = action$ =>
action$.pipe(
ofType(action1),
forkJoin(from(fetch("https://api.github.com/users"))).pipe(
map((response: any) => {
return action2;
})
)
);
我收到输入错误:
Argument of type 'Observable<{ type: string; }>' is not assignable to parameter of type 'OperatorFunction<{}, Action<any>>'.
我想知道为什么编译不通过?以及类型不匹配的原因,在这种情况下,是什么导致没有 forkJoin 的 epics 无效?
编辑:我知道 forkJoin 对于单个 observable 没有意义,但我放 1 是为了让示例更小
observable.pipe()
仅在其中包含 operators。
forkJoin 是一个 returns 一个 observable 的运算符,这就是你得到那个错误的原因。 Argument of type 'Observable' is not assignable to parameter of type 'OperatorFunction'
即 forkJoin,其中 returns 一个可观察对象不能分配给运算符函数的类型。
switchMap 是一个运算符,它 returns 一个 OperatorFunction 并在一个可观察对象上运行,这就是您的第一种方法起作用的原因。 switchMap(() => someObservable)
和 someObservable
在这种情况下是 forkJoin()
从您的导入中也可以看出这一点。您可能从 rxjs
库导入了 forkJoin,而从 rxjs/operators
库导入了 switchMap。