离子打字稿错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了离子打字稿错误相关的知识,希望对你有一定的参考价值。

我是Ionic框架的新手。我正在尝试如何在应用程序中集成指纹授权。

为此,我在我的home.ts文件中添加了以下代码:

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';

import { FingerprintAIO } from '@ionic-native/fingerprint-aio';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(public navCtrl: NavController, private fingerPrint: FingerprintAIO) {

  }

  this.fingerPrint.show({
    clientId: 'Fingerprint-Demo',
    clientSecret: 'password', //Only necessary for android
    disableBackup:true,  //Only for Android(optional)
    localizedFallbackTitle: 'Use Pin', //Only for ios
    localizedReason: 'Please authenticate' //Only for iOS
  })
  .then((result: any) => console.log(result))
  .catch((error: any) => console.log(error));
}

当我使用ionic cordova build android进行Android构建时,我收到以下错误:

typescript: src/pages/home/home.ts, line: 16 
            Unexpected token. A constructor, method, accessor, or property was expected. 

      L16:    this.fingerPrint.show({
      L17:      clientId: 'Fingerprint-Demo',

[15:24:38]  typescript: src/pages/home/home.ts, line: 25 
            Declaration or statement expected. 

      L24:    .catch((error: any) => console.log(error));

Error: Failed to transpile program

我该如何解决这个问题?

答案

将此代码放在constructor()中,就像这样,

constructor(public navCtrl: NavController, private fingerPrint: FingerprintAIO) {
      this.fingerPrint.show({
        clientId: 'Fingerprint-Demo',
        clientSecret: 'password', //Only necessary for Android
        disableBackup:true,  //Only for Android(optional)
        localizedFallbackTitle: 'Use Pin', //Only for iOS
        localizedReason: 'Please authenticate' //Only for iOS
      })
      .then((result: any) => console.log(result))
      .catch((error: any) => console.log(error));
}
另一答案
constructor(public navCtrl: NavController, private fingerPrint: FingerprintAIO) {
this.initFigerprint();
  }


initFigerprint(){

  this.fingerPrint.show({
    clientId: 'Fingerprint-Demo',
    clientSecret: 'password', //Only necessary for Android
    disableBackup:true,  //Only for Android(optional)
    localizedFallbackTitle: 'Use Pin', //Only for iOS
    localizedReason: 'Please authenticate' //Only for iOS
  })
  .then((result: any) => console.log(result))
  .catch((error: any) => console.log(error));
}

以上是关于离子打字稿错误的主要内容,如果未能解决你的问题,请参考以下文章

打字稿错误离子3

Swiper - 没有找到依赖打字稿,离子-vue

类型“typeof File”离子打字稿上不存在属性“moveFile”

离子 2 - 打字稿 |填充接口对象数组

如何在打字稿/离子2中按周分组时间戳?

修改返回数据,然后在打字稿/离子中返回承诺