简体   繁体   English

React Native (Typescript) 绝对路径无法解析模块

[英]React Native (Typescript) Absolute path unable to resolve module

I have just created a new project and I am trying to setup an absolute path by following this post: https://medium.com/geekculture/making-life-easier-with-absolute-imports-react-in-javascript-and-typescript-bbdab8a8a3a1我刚刚创建了一个新项目,我正在尝试通过以下帖子设置绝对路径: https://medium.com/geekculture/making-life-easier-with-absolute-imports-react-in-javascript-and -打字稿-bbdab8a8a3a1

I have followed the steps as close as I can but can not get this to work in my simulator.我已尽可能地按照这些步骤进行操作,但无法在我的模拟器中使用它。 (Visually there are no errors when looking at my code in IDE) (在IDE中查看我的代码时视觉上没有错误)

终端错误

eslint-config.json eslint-config.json

{
  "eslintConfig": {
    "extends": ["react-app", "airbnb"],
    "settings": {
      "import/resolver": {
        "node": {
          "paths": ["src"],
          "extensions": [".js", ".jsx", ".ts", ".tsx"]
        }
      }
    }
  }
}

tsconfig.json tsconfig.json

{
  "extends": "@tsconfig/react-native/tsconfig.json",     /* Recommended React Native TSConfig base */
  "compilerOptions": {
    "skipLibCheck": true,                                 /* Skip type checking all .d.ts files. */
    "baseUrl": "src",
  },
}

In this image you can see my file structure, the error on my simulator.在此图像中,您可以看到我的文件结构,我的模拟器上的错误。 And how I am importing it.以及我如何导入它。

在此处输入图像描述

I have two exported functions in my Text.tsx export function Title & export function P我的 Text.tsx 中有两个导出函数 export function Title & export function P

Any help is appreciated... Thank you!感谢您的帮助...谢谢!

I will not suggest you to go with this flow because it's very confusing.我不建议您使用此流程,因为它非常令人困惑。

Suppose you had imported this react/client thing.假设你已经导入了这个react/client的东西。

  • Is it the npm package that contains subpath是不是包含子路径的npm包
  • Is it my folder structure pathname是我的文件夹结构路径名吗

So I suggest you to go with the flow that most of the developers are using currently.因此,我建议您遵循大多数开发人员当前使用的流程。

import Text from "@/views/Text";

I made one demo for you also, please check https://codesandbox.io/s/sleepy-dew-wn2rsu我也为你做了一个演示,请查看https://codesandbox.io/s/sleepy-dew-wn2rsu

Note笔记

  • Use eslint-plugin-import for removing import errors.使用eslint-plugin-import删除导入错误。

create new package.json file under "src" folder and add { "name": "PATH_NAME" }在 "src" 文件夹下创建新的package.json文件并添加{ "name": "PATH_NAME" }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM