我发现的唯一类似问题是这个问题,但在这种情况下,我看不到如何导致循环依赖:
我有一个导出常量的文件,如下所示:
(选择数组版本用于在“选择”输入中使用,而另一个则防止在条件检查中键入错误)
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句