React native 是否可以删除导入上的相对路径?
在任何带有node.js的项目中,react,我设置了env变量:React native 是否可以删除导入上的相对路径?,react-native,expo,React Native,Expo,在任何带有node.js的项目中,react,我设置了env变量: NODE_PATH=src/ 然后我可以像这样做 import COLORS from 'Constants/Colors' 而不是 import COLORS from '../../../Constants/Colors' 我刚从react native和expo开始,我正试图找到解决方案 我尝试了添加env变量的相同方法,但它不起作用您可以使用库简化导入路径。它允许写入别名导入,而不是复杂的相对路径。安装方式 然后像
NODE_PATH=src/
然后我可以像这样做
import COLORS from 'Constants/Colors'
而不是
import COLORS from '../../../Constants/Colors'
我刚从react native和expo开始,我正试图找到解决方案
我尝试了添加env变量的相同方法,但它不起作用您可以使用库简化导入路径。它允许写入别名导入,而不是复杂的相对路径。安装方式
然后像这样在babel.config.js
文件中添加相对路径
module.exports = function(api) {
api.cache(true);
return {
presets: ['babel-preset-expo', 'module:metro-react-native-babel-preset' ],
plugins: [
[
'module-resolver',
{
alias: {
'@company' : './src/company.json',
'@components' : './src/components'
}
}
]
]
};
};
然后您可以使用这样的绝对路径
import TextLabel from './src/components/textLabel/index.js'
至
import TextLabel from '@components/textLabel/index.js'
您可以使用来使用别名imports@MahdiN,成功了!非常感谢。你能把它作为一个答案贴出来,这样我就可以投票并接受它作为解决方案吗?谢谢你,伙计,这很酷:)
import TextLabel from '@components/textLabel/index.js'