我try 使用绝对导入路径,而不是Expo和React Native的相对路径.

我查看了expo 的文件,却找不到答案...在react社区中搜索主题时,我找到了babel-plugin-module-resolver个,但Expo似乎已经在使用它,所以我更改了我的.babelrc,创建了一些别名:

{
  "presets": ["babel-preset-expo"],
  "env": {
    "development": {
      "plugins": [
        "transform-react-jsx-source",
        ["module-resolver", {
          "root": ["./app"],
          "alias": {
            "Components": "./app/components",
          }
        }]
      ]
    }
  }
}

但我犯了以下错误:

Unable to resolve module '@expo/vector-icons/glyphmaps/Entypo.json' 
from '/Users/eduardoleal/Code/lua/rook/node_modules/@expo/vector-icons/Entypo.js': 
Module does not exist in the module map or in these directories:   /Users/eduardoleal/Code/lua/rook/node_modules/@expo/vector-icons/node_modules/@expo/vector-icons/glyphmaps ,   /Users/eduardoleal/Code/lua/rook/node_modules/@expo/vector-icons/glyphmaps  This might be related to https://github.com/facebook/react-native/issues/4968 To resolve try the following:   
1. Clear watchman watches: 'watchman watch-del-all'.   
2. Delete the 'node_modules' folder: 'rm -rf node_modules && npm install'.   
3. Reset packager cache: 'rm -fr $TMPDIR/react-*' or 'npm start --reset-cache'.
ABI16_0_0RCTFatal -[ABI16_0_0RCTBatchedBridge stopLoadingWithError:] __34-[ABI16_0_0RCTBatchedBridge start]_block_invoke_2 _dispatch_call_block_and_release _dispatch_client_callout _dispatch_main_queue_callback_4CF __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ __CFRunLoopRun CFRunLoopRunSpecific GSEventRunModal UIApplicationMain main start 0x0 

有什么简单的方法可以导入绝对路径吗?

推荐答案

过了一段时间,我试着让它运转起来.我可以通过以下.babelrc项解决问题:

{
  "presets": ["babel-preset-react-native-stage-0/decorator-support"],
  "env": {
    "development": {
      "plugins": ["transform-react-jsx-source"]
    }
  },
  "plugins": [
    ["module-resolver", {
      "alias": {
        "react-native-vector-icons": "@expo/vector-icons",
        "@expo/vector-icons/lib/create-icon-set": "react-native-vector-icons/lib/create-icon-set",
        "@expo/vector-icons/lib/icon-button": "react-native-vector-icons/lib/icon-button",
        "@expo/vector-icons/lib/create-icon-set-from-fontello": "react-native-vector-icons/lib/create-icon-set-from-fontello",
        "@expo/vector-icons/lib/create-icon-set-from-icomoon": "react-native-vector-icons/lib/create-icon-set-from-icomoon",
        "@expo/vector-icons/lib/icon-button": "react-native-vector-icons/lib/icon-button",
        "@expo/vector-icons/glyphmaps": "react-native-vector-icons/glyphmaps",
        "$components": "./app/components",
        "$config": "./app/config",
        "$helpers": "./app/helpers",
        "$navigators": "./app/navigators",
        "$reducers": "./app/reducers",
        "$screens": "./app/screens",
        "$images": "./assets/images",
        "$fonts": "./assets/fonts",
        "$icons": "./assets/icons",
        "$videos": "./assets/videos",
      }
    }]
  ]
}

我把babel-preset-expo的内容复制到了我的.babelrc.这不是最好的解决方案...但它工作正常.

更多细节here

React-native相关问答推荐

AxiosError:Reaction Native EXPO开发版本中的网络错误

React Native在同一页面上使用多个不同的导航器

领带麦克风在Reaction-Native-Incall-Manager中不工作

导航到另一个屏幕后清空搜索屏幕

React native + react-query + onError 不捕获并处理错误?

axios 在react native 中给出 [AxiosError: Network Error]

如何解决Readonly<{}> 类型上不存在属性 navigation&

react-native-firebase crashlytics 未显示在 firebase 仪表板上

我如何知道我的代码是否作为 React Native 运行

redux-saga:如何以编程方式为yields 创建多个calls/side-effects?

react-native 中 android 和 iOS 的图像大小

如何解决此错误您可能需要适当的加载程序来处理此文件类型

React Native 有全局作用scope域吗?如果没有,我该如何模仿它?

当我导航到react-native 的新页面时,如何强制卸载组件?

如何将 Leaflet.js 与 react-native 一起使用

如何在react-native矢量图标中从多个文件导入图标?

React Native - 为什么执行这个函数?

如何隐藏 React Native NavigationBar

在 React-native 上加载图像时出错:Unexpected character

更改导航标题背景 colored颜色