英文:
Absolute Path in React Native with Expo
问题
以下是已翻译的部分:
"我正在尝试在我的React Native应用程序中实现绝对路径,但我无法使其正常工作。
这是我的babel.config.js文件:
module.exports = function (api) {
api.cache(true);
return {
presets: ["babel-preset-expo"],
plugins: [
["react-native-reanimated/plugin"],
[
"module-resolver",
{
alias: {
"@/redux/*": ["redux/*"],
"@/components/*": ["components/*"],
"@/pages/*": ["pages/*"],
"@/utils/*": ["utils/*"],
"@/services/*": ["services/*"],
"@/hooks/*": ["hooks/*"],
},
},
],
],
};
};
然后,我尝试这样实现:
import { roomActions } from "@/redux/room/roomSlice";
但我遇到了以下错误:
"无法解析模块 @/redux/room/roomSlice,来自 C:\Users...:@/redux/room/roomSlice 无法在项目或以下目录中找到:"
我尝试通过编辑 babel.config.js 文件来添加绝对路径。"
英文:
I am trying to implement absolute path in my React Native App but I could not make it work
Here is my babel.config.js
module.exports = function (api) {
api.cache(true);
return {
presets: ["babel-preset-expo"],
plugins: [
["react-native-reanimated/plugin"],
[
"module-resolver",
{
alias: {
"@/redux/*": ["redux/*"],
"@/components/*": ["components/*"],
"@/pages/*": ["pages/*"],
"@/utils/*": ["utils/*"],
"@/services/*": ["services/*"],
"@/hooks/*": ["hooks/*"],
},
},
],
],
};
};
And I try to implement like this
import { roomActions } from "@/redux/room/roomSlice";
and I get this error
Unable to resolve module @/redux/room/roomSlice from C:\Users...: @/redux/room/roomSlice could not
be found within the project or in these directories:
I tried to add absolute path with editing babel.config.js
答案1
得分: 1
我认为你的别名有些问题。
尝试使用以下代码更改别名,希望你能解决问题。
alias: {
"@/redux": "./src/redux",
"@/components": "./src/components",
"@/pages": "./src/pages",
"@/utils": "./src/utils",
"@/services": "./src/services",
"@/hooks": "./src/hooks"
},
英文:
I think you have some problem with an alias.
Try to change the alias with this code and hope you will get your resolved problem.
alias: {
"@/redux": [
"./src/redux"
],
"@/components": [
"./src/components"
],
"@/pages": [
"./src/pages"
],
"@/utils": [
"./src/utils"
],
"@/services": [
"./src/services"
],
"@/hooks": [
"./src/hooks"
],
},
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论