首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修复ngx-signaturepad中“无法读取未定义的属性'toDataURL‘”的问题

如何修复ngx-signaturepad中“无法读取未定义的属性'toDataURL‘”的问题
EN

Stack Overflow用户
提问于 2020-08-25 01:29:47
回答 1查看 253关注 0票数 0

我使用的是Angular 9的ngx-signaturepad。绘图部分工作得很好,但在drawComplete()中,我得到了这个错误"Cannot read property 'toDataURL‘of undefined“,因为this.signaturePad为null,我不知道如何修复它。任何想法都将不胜感激。

代码语言:javascript
复制
  import { Component, AfterViewInit, ViewChild } from '@angular/core';
  import { SignaturePad } from 'ngx-signaturepad/signature-pad';

  @Component({
    selector: 'ci-signature',
    template: '<signature-pad [options]="signaturePadOptions" (onBeginEvent)="drawStart()" (onEndEvent)="drawComplete()"></signature-pad>'
  })

  // Uses: https://www.npmjs.com/package/ngx-signaturepad
  export class SignatureComponent implements AfterViewInit {
    @ViewChild(SignaturePad, { static: false }) signaturePad: SignaturePad;

    public signaturePadOptions: Object = { // passed through to szimek/signature_pad constructor
      'minWidth': 5,
      'canvasWidth': 500,
      'canvasHeight': 300
    };

    ngAfterViewInit() {
      // this.signaturePad is now available

      this.signaturePad.set('minWidth', 5); // set szimek/signature_pad options at runtime
      this.signaturePad.clear(); // invoke functions from szimek/signature_pad API
    }

    drawComplete() {
      // will be notified of szimek/signature_pad's onEnd event
      console.log(this.signaturePad.toDataURL());
    }

    drawStart() {
      // will be notified of szimek/signature_pad's onBegin event
      console.log('begin drawing');
    }

  }
EN

回答 1

Stack Overflow用户

发布于 2020-08-25 07:59:43

@yurzui回答了这个问题,谢谢Yurzui!

以下是工作代码

代码语言:javascript
复制
  import { Component, AfterViewInit, ViewChild } from '@angular/core';
  import { SignaturePad } from 'ngx-signaturepad';

  @Component({
    selector: 'ci-signature',
    template: '<signature-pad [options]="signaturePadOptions" (onBeginEvent)="drawStart()" (onEndEvent)="drawComplete()"></signature-pad>'
  })

  // Uses: https://www.npmjs.com/package/ngx-signaturepad
  export class SignatureComponent implements AfterViewInit {
    @ViewChild(SignaturePad, { static: true }) signaturePad: SignaturePad;

    public signaturePadOptions: Object = { // passed through to szimek/signature_pad constructor
      'minWidth': 5,
      'canvasWidth': 500,
      'canvasHeight': 300
    };

    ngAfterViewInit() {
      // this.signaturePad is now available

      this.signaturePad.set('minWidth', 5); // set szimek/signature_pad options at runtime
      this.signaturePad.clear(); // invoke functions from szimek/signature_pad API
    }

    drawComplete() {
      // will be notified of szimek/signature_pad's onEnd event
      console.log(this.signaturePad.toDataURL());
    }

    drawStart() {
      // will be notified of szimek/signature_pad's onBegin event
      console.log('begin drawing');
    }

  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63565926

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档