我正在尝试使用绝对导入路径而不是使用Expo和React Native 的相对路径.
我查看了博客文档并找不到答案...在反应社区中搜索主题我发现了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",
}
}]
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
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
Run Code Online (Sandbox Code Playgroud)
有没有简单的方法来导入绝对路径?
如果没有root-元素,分离plugins,改变presets到babel-preset-react-native-stage-0/decorator-support,别名为我工作.
更新:Expo.io SDK v20.0.0的更改
从SDK v20.0.0开始,您可以使用普通的Babel Expo预设
module.exports = function(api) {
api.cache(true)
return {
presets: ['babel-preset-expo'],
plugins: [
[
'module-resolver',
{
alias: {
assets: './assets',
components: './src/components',
modules: './src/modules',
lib: './src/lib',
types: './src/types',
constants: './src/constants',
},
},
],
],
}
}
Run Code Online (Sandbox Code Playgroud)
Expo.io SDK v19.0.0及之前版本
在16.0.0版本上使用Expo.io
在世博会论坛中找到了这个:https://forums.expo.io/t/relative-paths-with-expo/654/3
您能否验证这也适用于您的情况?
{
"presets": ["babel-preset-expo"],
"env": {
"development": {
"plugins": ["transform-react-jsx-source"]
}
},
"plugins": [
["module-resolver", {
"alias": {
"alias-name": "./app",
}
}]
]
}
Run Code Online (Sandbox Code Playgroud)
除了Laszlo Schurg 的回答,以防有人在使用 Typescript 时遇到同样的问题。
如果您使用 Typescript,您还需要将其添加到您的tsconfig
"compilerOptions": {
...
"baseUrl": ".",
"paths": {
"@src/*": ["./src/*"],
"@assets/*": ["./assets/*"]
}
},
Run Code Online (Sandbox Code Playgroud)
这是我的配置babel.config
plugins: [
...
[
"module-resolver",
{
alias: {
"@assets": "./assets",
"@src": "./src",
},
},
],
],
Run Code Online (Sandbox Code Playgroud)
经过一段时间的努力,让这个工作正常进行。我可以通过以下方式解决问题.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",
}
}]
]
}
Run Code Online (Sandbox Code Playgroud)
我将 的内容复制babel-preset-expo到我的.babelrc. 这不是最好的解决方案......但它可以正常工作。
有关它的更多详细信息请参见此处
| 归档时间: |
|
| 查看次数: |
5893 次 |
| 最近记录: |