javascript - 由于从 index.js 导入需要循环
问题描述
我收到有关我的 react-native 项目的警告:
Require cycles are allowed, but can result in uninitialized values.
Consider refactoring to remove the need for a cycle.
Require cycle: src/navigation/index.js -> src/navigation/mainDrawerNavigator.js -> src/navigation/index.js
这是因为我有从index.js文件导出一个文件夹内的所有类的习惯,也从指定的index.js文件中导入(这很方便,虽然我不确定是否正确做法,但看起来虽然很常见:这里和这里)
我有以下目录结构:
src/
navigation/
index.js
mainDrawerNavigator.js
homeTabNavigator.js
otherNavigator.js
在index.js
(我同时导出mainDrawerNavigator
和homeTabNavigator
)
export {default as HomeTabNavigator} from './homeTabNavigator';
export {default as MainDrawerNavigator} from './mainDrawerNavigator';
export {default as OtherNavigator} from './otherNavigator';
问题在于mainDrawerNavigator
我需要homeTabNavigator
这样导入它
import {
HomeTabNavigator,
OtherNavigator,
} from '@/navigation'; // this is the source of the warning
有没有办法index.js
从不导致导入require cycle
?
我知道我可以这样导入:
import HomeTabNavigator from './homeTabNavigator';
import OtherNavigator from './otherNavigator';
但是对于某些文件夹,我在index.js
文件和文件夹中有很长的导出列表,我不想一个一个地导入(如果可能的话)
解决方案
推荐阅读
- html - 在 Angular-HTML 中使用 2 个管道
- mysql - 具有正确时间的 MySQL 出勤 IN & OUT 列
- android - 使用edittext将英文字符颜色更改为红色
- xamarin.forms - 来自 ViewModel 的 DisplayAlert 未显示
- python - 验证数据与训练数据 TensorFlow 2 分开?
- testing - 如何在 e2e 测试中跳过 MFA?
- docker - Docker ps 打印空列表
- javascript - 有没有办法通过一个 API 请求 Instagram Graph API 来获取所有包含信息的媒体?
- go - `gorm` 获取更新的 id
- el - ui:repeat:无论验证错误如何,内部组件都保持“有效”状态