react-native - typeScript中的导入组件出现问题

czfnxgou  于 2023-03-13  发布在  React
关注(0)|答案(1)|浏览(186)

当我的组件在./src/components/CustomButton/index.tsx中并且使用tsconfig中定义的路径时,我在导入组件时遇到问题。
但是,当我保存它不是在index.tsx和不使用目录,而只是在/components/CustomButton.tsx,然后import CutomButton from '../components/CustomButton.tsx'一切工作正常。
如何使第一版正常工作?
导入错误:enter image description here
自定义按钮索引.tsx

import {Button} from 'native-base';

const CustomButton = ({
  children,
  dark,
  onPress,
  isDisabled,
  bold,
  isLoading,
  mt,
}: any) => (
  <Button
    _text={{
      color: `${dark ? 'white' : 'black'}`,
      fontWeight: `${bold ? 'bold' : 'normal'}`,
      fontSize: 16,
    }}
    bg={dark ? 'muted.20' : 'muted.10'}
    colorScheme="muted"
    onPress={onPress}
    isDisabled={isDisabled}
    isLoading={isLoading}
    style={{borderRadius: 10}}
    p="4"
    mt={mt}>
    {children}
  </Button>
);

export default CustomButton;

配置

{
  "extends": "@tsconfig/react-native/tsconfig.json",
  "compilerOptions": {
    "target": "esnext",
    "module": "commonjs",
    "moduleResolution": "node",
    "outDir": "dist",
    "sourceMap": false,
    "jsx": "react-native",
    "allowJs": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "declaration": true,
    "noEmit": true,
    "isolatedModules": true,
    "strict": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "resolveJsonModule": true,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "baseUrl": "./src",
    "paths": {
      "@*": ["/*"],
      "@api/*": ["/apiActions/*"],
      "@components/*": [
        "/components/*"
      ],
      "@context/*": [
        "/Context/*"
      ],
      "@interfaces/*": [
        "/Interfaces/*"
      ],
      "@utils/*": [
        "/utils/*"
      ]
    }
  },
  "exclude": [
    "node_modules",
    "babel.config.js",
    "metro.config.js",
  ]
}

如何使第一个版本正常工作?我已经搜索了很多在这里和其他网页,但我没有找到答案

wgx48brx

wgx48brx1#

我已经有一段时间没有建立我React原生项目了。
我记不清了,但我相信这就是我的解决方案:
1.将babel-plugin-root-import添加到设备依赖项
yarn add -D babel-plugin-root-import
1.打开babel.config.js并在插件中添加babel-plugin-root-import

module.exports = function (api) {
   return {
      ...
      plugins: [
        // Add this line
         [
         'babel-plugin-root-import',
             {
                root: __dirname,
                rootPathPrefix: '@',
                rootPathSuffix: 'src',
             },
      ]
      ]
   }
}

如果记不起来,请告诉我,我会再记一次。
另一个解决方案是在你的文件夹中添加package.json,例如在components文件夹中添加package.json并写:

{
   "name": "@components"
}

此外,我相信您可以通过删除以下内容来更简洁地编写tsconfig.json文件

"@api/*": ["/apiActions/*"],
      "@components/*": [
        "/components/*"
      ],
      "@context/*": [
        "/Context/*"
      ],
      "@interfaces/*": [
        "/Interfaces/*"
      ],
      "@utils/*": [
        "/utils/*"
      ]
    }

在你的人生道路上改变成这样

"paths": {
   "@*": ["src/*"]
},

相关问题