首页 > 解决方案 > 如何将 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

标签: react-nativemetro-bundler

解决方案


您需要在 foo 文件夹中创建一个 index.js 文件

index.js

 import hello from './hello.js'

    export{
      hello
    }

您想将文件导入到哪里

其他文件.js

import hello from '../../src/foo'

推荐阅读