导入React Typescript的包时不会出现构造函数错误

问题描述:

我使用的是使用React with Typescript的项目。我试图导入一个包,当我运行该文件时得到的错误是该包没有构造函数。导入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> 
    ) 
} 
+0

你能分享你的进口陈述吗?我想知道你是否试图导入包没有默认导出的默认值? – Fenton

+0

@Fenton实际上是将其作为答案发布,因为修复是将'import * as jsPDF'而不是'import jsPDF'! – Eristikos

+0

我想这取决于jsPDF模块的内容,或者您​​只需要jsPDF的打字稿定义文件 – Kokodoko

当使用以下语法导入,你是依靠在那里是一个默认的导出在模块中:

import jsPDF from 'jspdf'; 

在许多情况下没有默认的出口(我喜欢的事情,这样,说实话,当你要明确你的依赖) - 所以你需要使用要么...

import * as jsPDF from 'jspdf'; 

或者导入...

import { justTheThingIWant } from 'jspdf'; 

(其中justTheThingIWant是你想要的东西的名字。