我从事一个将 React 与 Typescript 结合使用的项目。我正在尝试导入一个包,但在运行该文件时遇到的错误是该包没有构造函数。
我注意到很多包中都有这个错误,我想问问是否有办法让它们正常工作,而不是试图找到另一个具有构造函数的包。
我现在正在测试的包叫做jsPDF
Uncaught TypeError: jspdf_1.default is not a constructor
我的代码是
import jsPDF from 'jspdf';
export default class DataModelPage extends React.Component<any, any> {
public render(){
let doc = new jsPDF()
doc.text('Hello world!', 10, 10)
doc.save('a4.pdf')
return (
<div>...</div>
)
}
最佳答案
当使用以下语法导入时,您依赖于模块中的默认导出:
import jsPDF from 'jspdf';
在许多情况下,没有默认导出(老实说,我更喜欢这种方式,因为您可以明确说明您的依赖项)-因此您需要使用任何一种导入...
import * as jsPDF from 'jspdf';
或者...
import { justTheThingIWant } from 'jspdf';
(其中 justTheThingIWant 是您想要的东西的名称。
关于javascript - 为 React Typescript 导入包时不是构造函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46602927/