简体   繁体   English

导入文件而不添加.jsx 最后不起作用

[英]Import files without adding .jsx at the end not working

I am working on a react project in which I have imported .jsx files without adding the extension .jsx at the end.我正在处理一个 react 项目,在该项目中我导入.jsx文件,但没有在末尾添加扩展名.jsx But while I was trying to incorporate typescript files.但是当我试图合并 typescript 文件时。 It stopped identifying files unless the extension is added at the import statement.除非在导入语句中添加扩展名,否则它会停止识别文件。

import Item from "./Item"
//not working

import Item from "./Item.jsx"
//working

How can I fix this?我怎样才能解决这个问题? where could the problem be?问题可能出在哪里? I use create-react-app我使用 create-react-app

First of all, you have to add typescript into your project if you want to incorporate it.首先,如果您想将 typescript 添加到您的项目中,则必须将其合并。 Now for the extension problem, you can add a webpack.config.js file and include the following in it:现在对于扩展问题,您可以添加一个 webpack.config.js 文件并在其中包含以下内容:

{
    module.exports = {
        entry: './index.tsx',
        resolve: {
            extensions: ['', '.js', '.jsx', '.tsx']
        }
    }
}

this will enable you to import files without extensions.这将使您能够导入没有扩展名的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM