离子 2 框架签名板 formbuilder 问题

ionic 2 framework signature-pad formbuilder issue

当我尝试导航到基于 Ionic 2 框架的应用程序中的 mytest 页面时出现此错误:

Runtime Error Uncaught (in promise): TypeError: sp is not a constructor TypeError: sp is not a constructor at SignaturePad.ngAfterContentInit (http://localhost:8100/build/main.js:63958:29) at callProviderLifecycles (http://localhost:8100/build/main.js:11544:18) at callElementProvidersLifecycles (http://localhost:8100/build/main.js:11525:13) at callLifecycleHooksChildrenFirst (http://localhost:8100/build/main.js:11509:17) at checkAndUpdateView (http://localhost:8100/build/main.js:12526:5) at callViewAction (http://localhost:8100/build/main.js:12838:17) at execComponentViewsAction (http://localhost:8100/build/main.js:12784:13) at checkAndUpdateView (http://localhost:8100/build/main.js:12529:5) at callViewAction (http://localhost:8100/build/main.js:12838:17) at execEmbeddedViewsAction (http://localhost:8100/build/main.js:12810:17) at checkAndUpdateView (http://localhost:8100/build/main.js:12524:5) at callViewAction (http://localhost:8100/build/main.js:12838:17) at execComponentViewsAction (http://localhost:8100/build/main.js:12784:13) at checkAndUpdateView (http://localhost:8100/build/main.js:12529:5) at callWithDebugContext (http://localhost:8100/build/main.js:13511:42)

我在 angular2-signaturepad 节点模块的 SignaturePad.prototype.ngAfterContentInit 函数中放置了一些调试代码,看起来 canvas 对象是空的。

我做错了什么?

我的源代码:

app.module.ts:

...

import { SignaturePadModule } from 'angular2-signaturepad';
import { IonicStorageModule } from '@ionic/storage';

...

  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp),
    CloudModule.forRoot(cloudSettings),
    SignaturePadModule,
    IonicStorageModule.forRoot()
  ],

...

mytest.ts

...

import { SignaturePad } from 'angular2-signaturepad/signature-pad';
import { Storage } from '@ionic/storage';

...

@ViewChild('mySlider') mySlider: any;
@ViewChild(SignaturePad) signaturePad: SignaturePad;

  signaturePadOptions: Object = {
   'minWidth': 2,
   'canvasWidth': 400,
   'canvasHeight': 200,
   'backgroundColor': '#f6fbff',
   'penColor': '#666a73'
  };

  constructor(public viewCtrl: ViewController,
          public auth: Auth,
          public navCtrl: NavController,
          public navParams: NavParams,
          public events: Events,
          public tabsService: TabsService,
          public formBuilder: FormBuilder,
          public alert: AlertController,
          public storage: Storage) {
    this.slide2Form = formBuilder.group({ });
}
...

mytest.html

<ion-content>
<ion-slides #mySlider>
<ion-slide>
  <ion-list no-lines>
    <form [formGroup]="slide2Form">
      <ion-item>
        <ion-label>Date</ion-label>
        <ion-datetime displayFormat="MMMM/DD/YYYY" formControlName="signatureSigned"></ion-datetime>
      </ion-item>

      <ion-item>
        <signature-pad [options]="signaturePadOptions" (onBeginEvent)="drawStart()" (onEndEvent)="drawComplete()"></signature-pad>
        <button ion-button full color="danger" (click)="clearPad()">Clear</button>
      </ion-item>
    </form>
  </ion-list>
  </div>
</ion-slide>  
</ion-slides>                                  
</ion-content>  
...

您的代码似乎没问题,但 signature_pad 可能有问题, 也许你没有以正确的方式导入它,试试:

import * as SignaturePad from 'signature_pad';

从这里查看代码: https://github.com/wulfsolter/angular2-signaturepad/blob/master/signature-pad.ts

似乎 "sp" 正在尝试使用 "require"

加载模块

This link 也可能会阐明这个问题

我想通了这个问题。我正在导入 signature_pad 和 angular2-signaturepad 节点模块。尽管我没有使用 signature_pad 一个(它作为依赖项包含在 angular2-signaturepad 模块中),但它导致了上面的运行时错误。我删除了 signature_pad 模块:

npm uninstall signature_pad

然后我重新启动了该应用程序,它成功了。

在我的例子中,解决方案是更新到版本 2.6.0(我使用的是 2.2.0)

npm install angular2-signaturepad@2.6.0 --save