首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误TypeError: this.signaturePad.set不是函数

错误TypeError: this.signaturePad.set不是函数
EN

Stack Overflow用户
提问于 2021-09-01 02:26:51
回答 2查看 280关注 0票数 0

我正在用ngx-signaturepad开发一个angular6项目。我遵循了文档https://www.npmjs.com/package/ngx-signaturepad,但是我得到了错误'ERROR TypeError: this.signaturePad.set is not a function‘,并且没有在我的项目中显示记号板。

代码语言:javascript
复制
import { SignaturePadModule } from 'ngx-signaturepad';

...

@NgModule({
  declarations: [ ],
  imports: [ SignaturePadModule ],
  providers: [ ],
  bootstrap: [ AppComponent ]
})

// then import for use in a component

import { Component, ViewChild } from '@angular/core';
import { SignaturePad } from 'ngx-signaturepad/signature-pad';

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

export class SignaturePadPage{

  @ViewChild(SignaturePad) signaturePad: SignaturePad;

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

  constructor() {
    // no-op
  }

  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

回答 2

Stack Overflow用户

发布于 2021-09-01 02:52:56

我会尝试两件事。第一个方法是为模板中的元素分配一个ID,比如#signaturePad,然后在ViewChild声明中引用它。像这样的东西

代码语言:javascript
复制
@Component({
  template: '<signature-pad #signaturePanel [options]="signaturePadOptions" (onBeginEvent)="drawStart()" (onEndEvent)="drawComplete()"></signature-pad>'
})

export class SignaturePadPage{

  @ViewChild("signaturePanel") signaturePad: SignaturePad;
...

看起来问题在于你的变量没有正确初始化。这可能是由于错误的引用造成的,或者可能是组件显示后的初始化时间。您可以尝试在ngAfterViewInit中添加setTimeout,以检查是否发生了这种情况。如下所示:

代码语言:javascript
复制
ngAfterViewInit() {
setTimeout(()=>{
    this.signaturePad.set('minWidth', 5); // set szimek/signature_pad options at runtime
    this.signaturePad.clear(); // invoke functions from szimek/signature_pad API
},1000) //After 1 Second
}
票数 0
EN

Stack Overflow用户

发布于 2021-09-01 03:00:13

我怀疑当您尝试设置宽度时,可能没有完全初始化。利用onBeginEvent事件,而不是调用AfterViewInit方法。下面提供了一个示例。

代码语言:javascript
复制
import { SignaturePadModule } from 'ngx-signaturepad';

...

@NgModule({
  declarations: [ ],
  imports: [ SignaturePadModule ],
  providers: [ ],
  bootstrap: [ AppComponent ]
})

// then import for use in a component

import { Component, ViewChild } from '@angular/core';
import { SignaturePad } from 'ngx-signaturepad/signature-pad';

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

export class SignaturePadPage{

  @ViewChild(SignaturePad) signaturePad: SignaturePad;

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

  constructor() {
    // no-op
  }

  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');
    this.configureSignaturePad();
  }

  private configureSignaturePad() {
    // 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
  }
}```
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69007146

复制
相关文章

相似问题

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