【问题标题】:ERROR TypeError: this.signaturePad.set is not a function错误类型错误:this.signaturePad.set 不是函数
【发布时间】:2021-10-30 13:34:12
【问题描述】:

我正在使用 ngx-signaturepad 开发一个 angular6 项目。我遵循了文档https://www.npmjs.com/package/ngx-signaturepad,但我收到了错误 '错误类型错误:this.signaturePad.set 不是函数 ' 并且没有在我的项目中显示标志板。


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');
  }
}

【问题讨论】:

    标签: angular typescript angular6 signaturepad


    【解决方案1】:

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

    @Component({
      template: '<signature-pad #signaturePanel [options]="signaturePadOptions" (onBeginEvent)="drawStart()" (onEndEvent)="drawComplete()"></signature-pad>'
    })
    
    export class SignaturePadPage{
    
      @ViewChild("signaturePanel") signaturePad: SignaturePad;
    ...
    

    似乎问题在于您的变量未正确初始化。这可能是因为引用错误,或者可能是组件显示后的初始化时间。您可以尝试将 setTimeout 添加到 ngAfterViewInit 以检查是否发生这种情况。像这样的:

    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
    }
    

    【讨论】:

      【解决方案2】:

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

      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
        }
      }```
      

      【讨论】:

      • 签名板在这两种方法中都不可见
      猜你喜欢
      • 2018-04-28
      • 2019-02-14
      • 2021-10-02
      • 2018-11-22
      • 2017-10-12
      • 2020-06-05
      • 2023-03-15
      • 2018-10-21
      • 2020-10-20
      相关资源
      最近更新 更多