reactjs TS2691:导入路径不能以“.tsx”扩展名结尾,请考虑导入“./components/header.js”

uemypmqf  于 2023-05-06  发布在  React
关注(0)|答案(1)|浏览(647)

我用npx create-react-app myApp --template typescript命令创建了我的第一个typescript项目,但是我得到了错误TS2691: An import path cannot end with a '.tsx' extension. Consider importing './components/header.js' instead.。我已经在互联网上寻找这个错误,但我没有找到任何东西。
我也得到了错误ERROR in src/components/header.tsx TS1208: 'header.tsx' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.
App.tsx:

import './App.css';
import Header from './components/header.tsx';
import Main from './components/main.tsx';
import Footer from './components/footer.tsx';

function App() {
  return (
    <>
      <Header></Header>
      <Main></Main>
      <Footer></Footer>
  </>
  );
}

export default App;

footer.tsx:

function Footer(){
    return (
        <footer>
            <p>Desenvolvido por Priscila Alves, e-mail: priscila.contato@live.com</p>
        </footer>
    );
}
export default Footer;

package.json:

{
  ...
  "scripts": {
    "start": "react-scripts start",
    "build": "tsc",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  ...
4bbkushb

4bbkushb1#

这个配置到你的webpack配置文件:

{
  resolve: {
    extensions: [".js", ".json", ".ts", ".tsx"],
  },
}

将这些规则添加到您的打印脚本中-eslint

module.exports = {
  settings: {
    'import/resolver': {
      'node': {
        'extensions': ['.js','.jsx','.ts','.tsx']
      }
    }
  }
};

相关问题