问题
I'm currently working on an Angular2 project and I'm trying to use a JS library (https://github.com/szimek/signature_pad) for signature input.
I've tried using the library as is, with my code as follows:
// .ts file
import * as SignaturePad from 'signature_pad';
export class ... {
private signaturePad: SignaturePad;
ngOnInit() {
let canvas = document.querySelector("canvas");
this.signaturePad = new SignaturePad(canvas);
}
}
.
// .html file
<div...>
<canvas></canvas>
</div>
and I get the following error when I launch the browser page: ERROR Error: Uncaught (in promise): TypeError: WEBPACK_IMPORTED_MODULE_1_signature_pad is not a constructor
I've also tried using dimpu's angular2-signature-pad, but the same error essentially comes up.
回答1:
Provided you have the typings for signature_pad
(npm install --save-dev @types/signature_pad
), it has to be imported and used like below as signature_pad
does not have any exported members and only declares the class SignaturePad
into the current scope:
import 'signature_pad';
// ...
let canvas = document.querySelector("canvas");
this.signaturePad = new SignaturePad(canvas);
来源:https://stackoverflow.com/questions/44172211/typescript-typeerror-webpack-imported-module-1-signature-pad-is-not-a-con