离子2框架签名垫formbuilder问题

时间:2017-06-03 04:08:38

标签: canvas ionic2 html5-canvas angular2-formbuilder

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

  

运行时错误   未捕获(在promise中):TypeError:sp不是构造函数TypeError:sp不是SignaturePad.ngAfterContentInit(http://localhost:8100/build/main.js:63958:29)的构造函数,位于callEviderLifecycles(http://localhost:8100/build/main.js:11544:18),callElementProvidersLifecycles(http://localhost:8100/build/main.js:11525:13)at callLifecycleHooksChildrenFirst(http://localhost:8100/build/main.js:11509:17)在checkAndUpdateView(http://localhost:8100/build/main.js:12526:5)的callViewAction(http://localhost:8100/build/main.js:12838:17),在execComponentViewsAction(http://localhost:8100/build/main.js:12784:13),在checkAndUpdateView(http://localhost:8100/build/main.js:12529:5)的callViewAction(http://localhost:8100/build/main.js:12838:17)在checkAndUpdriteViews(http://localhost:8100/build/main.js:12810:17)的execEmbeddedViewsAction(http://localhost:8100/build/main.js:12524:5)atc的调用主题查看(http://localhost:8100/build/main.js:12838:17),在checkAndUpdateView(http://localhost:8100/build/main.js:12784:13)的callAndUpdateView(http://localhost:8100/build/main.js:12529:5),在callWithDebugContext (http://localhost:8100/build/main.js:13511:42

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

我做错了什么?

我的源代码:

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>  
...

3 个答案:

答案 0 :(得分:2)

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

import * as SignaturePad from 'signature_pad';

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

似乎“sp”正在尝试使用“require”

加载模块

This link也可能会对这个问题有所了解

答案 1 :(得分:0)

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

npm uninstall signature_pad

然后我重新启动了应用程序并且它运行了。

答案 2 :(得分:0)

在我的情况下,解决方案是更新到版本2.6.0 (我使用的是2.2.0)

npm install angular2-signaturepad@2.6.0 --save