CRA 上带有“@”的绝对导入不起作用

问题描述 投票:0回答:1

我尝试将应用程序从相对导入迁移到绝对导入。根据 CRA 文档,我将这些代码添加到我的 tsconfig.json 文件中:

{
  "compilerOptions": {
    "baseUrl: "src"
  },
  "include": ["src"]
}

此设置效果很好,例如:

import MyComponent from "components/MyCopmonent"

但我想用“@”设置导入,如下所示:

import MyComponent from "@components/MyComponent"

reactjs typescript create-react-app tsconfig
1个回答
0
投票

我假设您正在尝试设置路径别名,因此您需要这样配置它们

{
  "compilerOptions": {
    "baseUrl": ".", // Root of your paths
    "paths": {
      "@components/*": ["src/components/*"] // enables to use @components/MyComponent
    }
  }
}
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.