我正在尝试映射 tsconfig.json 中的路径以摆脱 相对路径 hell 。我的 React App 基于 Create-React-App .
我试过这个 SO thread并在我的 tsconfig.json 中添加了路径。我的 tsconfig.json 是
{
"compilerOptions": {
"baseUrl": "src",
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react",
"strictNullChecks": false
},
"include": [
"src"
]
}
当我在 VS Code 中编译我的项目时,它会从 tsconfig.json 中删除路径条目,并显示以下消息。为什么我的 不支持别名导入 react 脚本 基于 React 项目?最佳答案
不再支持路径别名
你现在可以这样做,直接导入相对于 src
的文件目录
转到您的 jsconfig.json
文件添加基本 URL 为 "."
"compilerOptions": {
"baseUrl":".",
...
那么你可以直接从 src
导入东西目录import Myfile from "src/myfile.js"
关于reactjs - 不得设置 compilerOptions.paths(不支持别名导入),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64172923/