react-native - 如何将 src 文件夹添加到 React-native 并绝对导入?
问题描述
我正在尝试添加src
目录,以便我有类似的东西
- package.json
- android
- ios
- src
- foo
- hello.js
- index.js
- App.js
- node_modules
然后我加了
watchFolders: [path.join(__dirname, 'src')],
至metro.config.js
希望能够import hello from 'foo/hello'
从任何地方(例如从App.js
)做
我还发现absolute import
如果你在下面添加文件夹,你可以实现node_modules
- package.json
- android
- ios
- node_modules
- foo
- hello.js
- index.js
- App.js
解决方案
您需要在 foo 文件夹中创建一个 index.js 文件
index.js
import hello from './hello.js'
export{
hello
}
您想将文件导入到哪里
其他文件.js
import hello from '../../src/foo'
推荐阅读
- html - 如何解决:网站页面在线显示问号,但在 localhost 中不显示
- r - 将列表分配给列属性
- python - Python散点图与绘图
- java - 导入 org.apache.cxf 无法解析Java
- javascript - 使用 jQuery Datepicker 使日期在加载时无法选择
- python - pyqt5 设计器第二个窗口按钮不起作用
- r - 我在数据框的下标分配中不允许缺失值有错误
- azure - Azure:无法将存档 blob 从一个存储帐户复制到另一个?
- java - 如何调用字符串类型的方法?
- amazon-web-services - 无法使用 s3 bitbucket 在 ec2 中部署代码