我有以下代码:
export const googleTagManagerInternalActionsEpic: Epic<RootAction, RootAction, RootState, Services> = (action$, state$) => {
return action$.pipe(
mergeMap(action => merge(
of(action).pipe(
filter(isActionOf(triggerGTMEvent)),
tap(({ payload: eventName }) => {
console.log(eventName);
}),
mergeMap(() => loadQuestion()),
map(questionImport => questionImport.default),
tap((question) => {
console.log(question);
debugger;
}),
ignoreElements(),
),
)),
);
}
我需要在第二个console.log(...)
上访问与第一个console.log(...)
上相同的eventName
值。我这里的问题是我之间的mergeMap
(这是防止有效载荷的传播)。为了从loadQuestion()
调用中获得一些信息,我被迫使用mergeMap
。
是否有办法让我得到两个值?
谢谢!
您可以使用第二个参数在mergeMap:
...
tap(({ payload: eventName }) => {
console.log(eventName);
}),
mergeMap(() => loadQuestion(), (eventName, questionImport) => ({
eventName,
questionImport
})),
map(({ eventName, questionImport }) => ({
eventName,
question: questionImport.default
})),
tap(({ eventName, question }) => {
console.log(eventName, question);
debugger;
}),
...