ES6 模块 - 如果不在 React 组件中,则导入的常量未定义

ES6 modules - imported constants undefined if not in React component

我发现的唯一类似问题是 但我看不出在这种情况下我会如何导致循环依赖:

我有一个导出 常量 的文件,如下所示:

(选择数组版本用于在 Select 输入中使用,另一个用于防止在条件检查中输入错误)

payments.constants.js

export const paymentMethodChoices = [
    { id: "Cash", name: "Cash" },
    { id: "BankTransfer", name: "BankTransfer" },
];

export const paymentMethods = {
    Cash: paymentMethodChoices[0],
    BankTransfer: paymentMethodChoices[1],
}

当它们被导入到我的任何 react 组件中时,一切都按预期工作。

MyReactComponent.js

import React from 'react';
import { paymentMethods } from '../../../constants';

const defaultValues = () => {    
    console.log("const object is available", paymentMethods)
    
    return {
        paymentMethod: paymentMethods.Cash.id,
        /* ... other scalar values*/
    }
};

const MyReactComponent = (props) => { ... }

但是当我尝试在另一个 js 文件中导入常量并将它们合并到另一个常量中时,我收到一条错误消息,说它们是 undefined:

defaultValues.js

import { paymentMethods } from '../../../../constants';

export const dailyCostCalendarDefaultValues = {    
    paymentMethod: paymentMethods.Cash.id,
    vatReturn: true,
};

错误消息:TypeError: Cannot read property 'Cash' of undefined

好吧,最后这确实是一个循环依赖,但由于文件导入链很长,所以非常复杂。类似于:

- external.js - file where the parent.js is imported    
|
 ... - parent.js - deeply nested parent file importing fileWithProblem.js
    |
     -- fileWithProblem.js - importing external.js

有时 export const () => {} 不工作。

我已经通过像 export function () {} 格式更改它们来修复。

之后它工作正常,但我不明白为什么会这样。