Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/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 如何配置Expo以允许全局模块导入?_React Native_Expo - Fatal编程技术网

React native 如何配置Expo以允许全局模块导入?

React native 如何配置Expo以允许全局模块导入?,react-native,expo,React Native,Expo,我在想办法摆脱这些 import { get_records } from ../../../../store/actions/async'; 老实说,我尝试了很多技巧: 使用src/中的package.json和{“name”:“@app”}并尝试使用: import { get_records } from '@app/store/actions/async'; 使用React本机AppRegistry名称: import { get_records } from 'main/sr

我在想办法摆脱这些

import { get_records } from ../../../../store/actions/async';
老实说,我尝试了很多技巧:

  • 使用
    src/
    中的
    package.json
    {“name”:“@app”}
    并尝试使用:

    import { get_records } from '@app/store/actions/async';
    
  • 使用React本机AppRegistry名称:

     import { get_records } from 'main/src/store/actions/async';
    
同样的问题也适用于资产

我见过Webpack和Babel的解决方案,但我没有在React Native中使用它们


您是如何做到这一点的?

如果您使用的是Expo,您必须使用babel,因为只有ES6版本的库才提供。默认情况下,即使是使用
react native init创建的项目也会附带babel。检查项目文件夹,它应该有一个
.babelrc
文件

然后,您可以使用:

.LRC:

{
  "presets": ["babel-preset-expo"],
  "plugins": [
    [
      "babel-plugin-root-import",
      {
        "rootPathSuffix": "src"
      }
    ]
  ]
}

实际上我在不知不觉中使用了巴贝尔

npm install --save-dev babel-plugin-module-resolver
npm install --save-dev eslint-import-resolver-babel-module
.babelrc

{
  "presets": ["babel-preset-expo"],
  "env": {
    "development": {
      "plugins": ["transform-react-jsx-source"]
    }
  },
  "plugins": [
    ["module-resolver", {
      "alias": {
        "~mobile": "./src",
        "~assets": "./assets"
      }
    }]
  ]
}
{
    "settings": {
      "import/resolver": {
        "babel-module": {
          "alias": {
            "~mobile": "./src",
            "~assets": "./assets"
          }
        }
      }
    }
}
.eslintrc

{
  "presets": ["babel-preset-expo"],
  "env": {
    "development": {
      "plugins": ["transform-react-jsx-source"]
    }
  },
  "plugins": [
    ["module-resolver", {
      "alias": {
        "~mobile": "./src",
        "~assets": "./assets"
      }
    }]
  ]
}
{
    "settings": {
      "import/resolver": {
        "babel-module": {
          "alias": {
            "~mobile": "./src",
            "~assets": "./assets"
          }
        }
      }
    }
}
然后我就这样使用它:

import { addToFavorite, removeFromFavorite } from '~mobile/store/actions';

await Font.loadAsync({
  'cabin-reg': require('~assets/fonts/Cabin/Cabin-Regular.ttf'),
  'league-spartan': require('~assets/fonts/LeagueSpartan/leaguespartan-bold.ttf')
 })

你确定你没有在RN上使用Babel吗?@riwu你是对的,谢谢你的提示!