绝对路径在React Native中使用Expo

huangapple go评论57阅读模式
英文:

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"
    ],
  },

huangapple
  • 本文由 发表于 2023年2月8日 12:27:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75381385.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定