简体   繁体   English

使用 index.js 循环导入的问题?

[英]Problems with circular imports using index.js?

Given the following file structure:给定以下文件结构:

myFiles
├── index.js
├── getTrue.js
└── dependentGetFalse.js

And the following code以及以下代码

// index.js
export { getTrue } from './getTrue'
export { dependentGetFalse } from './dependentGetFalse'
// getTrue.js
export const getTrue = () => true
// dependentGetFalse.js
import { getTrue } from '.'
export const dependentGetFalse = () => !getTrue()

Where there's (what I assume to be) a circular import between dependentGetFalse.js and index.js .dependentGetFalse.jsindex.js之间存在(我假设是)循环导入的地方。

What problems will arise from this?这样做会产生什么问题? Or is it ok to have?还是可以拥有?

If your codes run flawless and you are comfortable with them, It is ok to have this circular situation如果您的代码运行完美,并且您对它们感到满意,则可以出现这种循环情况

It's best to avoid using '.'最好避免使用'.' imports.进口。

Try this:尝试这个:

// dependentGetFalse.js
import { getTrue } from './getTrue'
export const dependentGetFalse = () => !getTrue()

index.js is useful when you try to import something from outside of this folder.当您尝试从该文件夹之外导入某些内容时, index.js很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM