- yarn add -D babel-plugin-root-import
- babel.config.js 添加 plugins
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
plugins: [
[
'babel-plugin-root-import',
{
paths: [
{
rootPathPrefix: '~',
rootPathSuffix: 'src',
},
],
},
],
],
};
- 在项目根目录下添加jsconfig.json
{
"compilerOptions": {
"jsx": "react-native",
"allowSyntheticDefaultImports": false,
"experimentalDecorators": true,
"baseUrl": ".",
"paths": {
"~/*": ["src/*"]
}
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
- 在项目中愉快的使用
import env from '~/constants/env';