簡體   English   中英

如何在不同文件中的史詩之間共享變量?

[英]How to share a variable between epics that are in different files?

我在我的nextjs Web 應用程序中使用redux-observable 有一個變量必須在epic1設置並稍后在epic2訪問:

let sharedVariable;

const epic1 = (action$, state$) => {
    return action$.pipe(
        // check if the dispatched action is of type action1
        filter(action1.match),
        tap((action) => {
            // create a new instance of A and assign
            // it to sharedVariable
            if (!sharedVariable) {
                sharedVariable = new A();
            }
        }),
        mergeMap((action) => {
            ...
        }),
    )
}

const epic2 = (action$, state$) => {
    return action$.pipe(
        // check if the dispatched action is of type action2
        filter(action2.match),
        map((action) => {
            if (sharedVariable) {
                return action3();
            } else {
                return action4();
            }
        }),
    )
}

我想將史詩放在不同的文件中: epics1.jsepics2.js

當史詩位於不同的文件時,我應該如何啟用它們來設置和訪問sharedVariable

提前致謝!

聽起來像是 DI 的好案例: docs

const sharedVariable = {} // can not be primitive type
const epicMiddleware = createEpicMiddleware({
  dependencies: { sharedVariable }
});

const epic2 = (action$, state$, { sharedVariable }) => {
 // Rest of code
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM