我正在尝试使我的React应用程序中的导入文件路径绝对。
(编辑:我正在尝试这样做:
import fetchAPI from "../../util/api"
对此
import fetchAPI from "util/api"
)
我已经看到了使用jsconfig.json这样的解决方案
{
"compilerOptions": {
"baseUrl": "src"
},
"include": ["src"]
}
按照我的理解,这只会使.js文件中的文件路径成为绝对路径。不幸的是,项目中绝大多数文件都是.jsx,有没有办法使文件路径成为绝对路径?
预先感谢:)
答案 0 :(得分:1)
一种解决方法是将target
选项更改为"target": "esnext"
。
然后更改
compilerOption
定位到React或Reactn-native,例如"jsx": "preserve"
或"jsx": "react"
或"jsx": "react-native"
。
{
"compilerOptions": {
"target": "esnext",
"jsx": "react",
"baseUrl": "src"
},
"exclude": [
"node_modules",
"**/node_modules/*"
]
}