2017-10-21 74 views
0

我在「離子世界」新的和我想使用這個插件在我的應用程序離子:https://www.npmjs.com/package/cordova-plugin-imei如何在Ionic 3上使用cordova插件?

然而,當我嘗試使用我的插件控制器上,它不承認插入。下面是一段代碼:

obterIMEI(){ 
var permissions = cordova.plugins.permissions; 
var imei = cordova.plugins.imei; 
permissions.requestPermission(permissions.READ_PHONE_STATE, success, error); 
function error() { 
    this.msgOptions.message = 'Não há permissões para acessar o IMEI!'; 
    this.msg.create(this.msgOptions).present();  
} 
function success(status) { 
    imei.get(
     function (imei) { 
      console.log('worked'); 
      this.imei = imei; 
     }, 
     function() { 
      console.log('error'); 
      this.msgOptions.message = 'Erro ao acessar o IMEI!'; 
      this.msg.create(this.msgOptions).present();        
     } 
    ); 
} 

}

線條cordova.plugins.permissionswindow.plugins.imei.get不承認,當我嘗試運行應用程序,我得到以下錯誤:

[10:20:54] typescript: src/pages/imei/imei.ts, line: 27 
     Cannot find name 'cordova'. 

    L26: obterIMEI(){ 
    L27: var permissions = cordova.plugins.permissions; 
    L28: permissions.requestPermission(permissions.READ_PHONE_STATE, success, error); 

[10:20:54] typescript: src/pages/imei/imei.ts, line: 35 
     Property 'plugins' does not exist on type 'Window'. 

    L34: function success(status) { 
    L35:  window.plugins.imei.get(
    L36:   function (imei) { 

我已經安裝了使用終端的插件,但它不起作用。我錯過了什麼?提前致謝。

回答

0

聲明一個Ambient變量並使用它。例如:

// Global ambient variable 
declare var cordova: any; 

function buscarImei() { 
    var permissions = cordova.plugins.permissions; 
    var imei = cordova.plugins.imei; 
    permissions.requestPermission(permissions.READ_PHONE_STATE, success, error); 
    function error() { 
     alertify.error("Sem Permissão para a busca de IMEI!"); 
    } 
    function success(status) { 
     imei.get(
      function (imei) { 
       // NOTE : This is Ionic 1 syntax. You need to check for accessing in Ionic 2/3 (Scope variable depreciated from ionic 2 onwards) 
       $scope.config.imei = imei; 
      }, 
      function() { 
       alertify.error("Erro ao carregar IMEI!!"); 
      } 
     ); 
    } 
}; 
+0

我添加了變量,並改變了我的功能,因爲你做了,我得到這個錯誤:'ReferenceError:cordova is not defined'。我要在這個問題上更新我的代碼。 –

+0

是的,你會得到「cordova沒有定義的錯誤」,因爲你會將離子應用加載到你使用的插件不支持的瀏覽器上。構建Apk並嘗試在移動設備上運行應用程序。它應該可以正常工作。 –

相關問題