简体   繁体   English

有什么方法可以在React Redux项目中使用Typescript文件?

[英]Is there any way to use typescript file in react redux project?

I got a completely developed project in react,redux and typescript. 我得到了一个在React,Redux和Typescript上完全开发的项目。 Le me say it project "A". 我说它是项目“ A”。 I am working on project "B" with react and redux using .jsx extention. 我正在使用.jsx扩展使用React和Redux开发项目“ B”。 Now I want to reuse a module from project A(.ts extension files) in project B. How can I use .ts files along with jsx files in project B? 现在,我想重用项目B中项目A(.ts扩展文件)中的模块。如何在项目B中将.ts文件和jsx文件一起使用?

You'd have to add a typescript loader to your webpack config, so webpack knows it can also load in ts files. 您必须在Webpack配置中添加一个Typescript加载器,因此Webpack知道它也可以加载到ts文件中。

Now simply require() or import the file (depends on which one you already use) in javascript. 现在只需在JavaScript中使用require()或导入文件(取决于您已使用的文件)即可。

Make sure you don't use default exports but named exports, cause default export works a bit different in require() vs. import .. from. 确保您不使用默认导出,而是使用命名导出,因为默认导出在require()与import .. from中的工作方式有所不同。

On the other hand you can always make project A generate the JS file for you and use that one in proj B. 另一方面,您始终可以使项目A为您生成JS文件,并在项目B中使用该文件。

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

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