ionic2电话呼叫功能显示错误:意外令牌。期望构造函数,方法,访问器或属性

时间:2017-07-26 02:06:21

标签: angularjs ionic2

about.html 这里是html页面

<ion-content>    
    <ion-fab center buttom>
      <button ion-fab color="light">
        <ion-icon name='call'></ion-icon>
      </button>
    </ion-fab>

    <!-- <a ion-button color="light" href="tel:1800889958">
    立即拨打
    </a>   -->  

</ion-content>

about.ts

这里是打字稿页面。我安装了电话号码插件ald,并导入到app.module.ts。

错误显示在'this'

错误消息:意外令牌。期望构造函数,方法,访问器或属性。

import { Component } from '@angular/core';
import { CallNumber } from '@ionic-native/call-number';


@Component({
  selector: 'page-about',
  templateUrl: 'about.html'
})

export class AboutPage {

  constructor(private call: CallNumber) { }

  this.callNumber.callNumber("1800889958", true)
  .then(() => console.log('Launched dialer!'))
  .catch(() => console.log('Error launching dialer'));

}

CLI错误显示:

[09:56:04]  typescript: src/pages/about/about.ts, line: 14 
            Unexpected token. A constructor, method, accessor, or property was expected. 

      L14:    this.callNumber.callNumber("1800889958", true)
      L15:    .then(() => console.log('Launched dialer!'))

[09:56:04]  typescript: src/pages/about/about.ts, line: 18 
            Declaration or statement expected. 

1 个答案:

答案 0 :(得分:1)

您必须添加CallNumber作为提供者。

import { CallNumber } from '@ionic-native/call-number';

...

@NgModule({
  ...

  providers: [
    ...
    CallNumber
    ...
  ]
  ...
})
export class AppModule { }

你的about.ts应该是这样的:

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { CallNumber } from '@ionic-native/call-number';


@Component({
  selector: 'page-about',
  templateUrl: 'about.html'
})

export class AboutPage {

  constructor(private call: CallNumber, private platform: Platform) { 
     this.platform.ready().then(() => {
        this.callToNumber();
     });
  }

  private callToNumber() {
     this.call.callNumber("1800889958", true)
         .then(() => console.log('Launched dialer!'))
         .catch(() => console.log('Error launching dialer'));  
  }  

}

检查此行this.call.callNumber,您有this.callNumber.callNumber,这似乎是个问题。

更新

当您想要使用本机功能时,请始终等待Platform.ready()承诺,因为如果您尝试使用原生apis做某事,它将失败。