简体   繁体   English

如何使用打字稿在react-native应用程序中正确配置绝对导入?

[英]How to configure correctly absolute imports in react-native app with typescript?

I am trying to configure absolute imports in the application based on react-native init MyApp --template typescript 我正在尝试基于react-native init MyApp --template typescript在应用程序中配置绝对导入

Here is my project structure: 这是我的项目结构:

root
    -src
      -components
            -Header.tsx

      -screens
            -Categories.tsx
package.json
babel.config.js
tsconfig.json
...

I am trying to import Header from Categories as 我正在尝试从类别中导入标题为

import Header from 'components/Header'

but it can't resolve a path... 但它无法解析路径...

在此处输入图片说明

I tried a lot of configurations and here is my latest: 我尝试了很多配置,这是我的最新配置:

babel.config.json babel.config.json

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    ['module-resolver', {
      'root': ['./src'],
      'extensions': ['ts', 'tsx', '.js', '.jsx', '.es', '.es6', '.mjs']
    }]
  ]
};

tsconfig.json tsconfig.json

...
"baseUrl": ".",
    "paths": {
      "*": ["./src/*"]
    },

It doesn't work. 没用

react-native --reset-cache

doesn't work for me too. 也不适合我 Help :) 救命 :)

要获取类别,应将其移至上一个文件夹。

import Header from '../components/Header'

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

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