Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 是否可以删除导入上的相对路径?_React Native_Expo - Fatal编程技术网

React native 是否可以删除导入上的相对路径?

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.js的项目中,react,我设置了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'