gpt4 book ai didi

reactjs - 导入路径不能以 '.tsx' 扩展名结尾

转载 作者:行者123 更新时间:2023-12-05 05:59:51 25 4
gpt4 key购买 nike

我是 Typescript 的新手,正在尝试为 TypeScript 构建一个 React 样板。但不能导入形式为“.tsx”的组件。

我的 tsconfig 文件如下所示:

{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": [
"src"
]
}

我的 eslintrc 看起来像这样:

{
"root": true,
"env": {
"browser": true,
"es2021": true
},
"extends": [
"plugin:react/recommended",
"airbnb"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 12,
"sourceType": "module"
},
"plugins": [
"react",
"@typescript-eslint"
],
"rules": {
"react/jsx-filename-extension": [2, { "extensions": [".js", ".jsx", ".ts", ".tsx"] }],
"linebreak-style": 0,
"global-require": 0,
"eslint linebreak-style": [0, "error", "windows"],
"no-use-before-define": "off",
"indent": [2, 2]
}
}

最佳答案

您可以像这样编辑 tsconfig.json 文件并添加路径选项:

{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"noImplicitAny": false,
"paths": {
"@/*": ["src/*"]
},
},
"include": ["**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}

关于reactjs - 导入路径不能以 '.tsx' 扩展名结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67926317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com