参考误差:cordova未定义离子

时间:2017-06-26 16:56:03

标签: javascript cordova ionic-framework

首先我用

创建了空白项目
ionic start checkGPS blank

之后我安装了插件 -

cordova.plugins.diagnostic

我在www / index.html

中添加了以下代码
cordova.plugins.diagnostic.isGpsLocationEnabled(function(enabled){
    console.log("GPS location is " + (enabled ? "enabled" : "disabled"));
}, function(error){
    console.error("The following error occurred: "+error);
})

查看config.xml&的package.json 插件信息已添加,即使我尝试添加cordova.js

但它虽然没有奏效!

的index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>
    <link rel="manifest" href="manifest.json">
    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
    <script src="lib/ionic/js/ionic.bundle.js"></script>
    <script src="cordova.js"></script>
    <script src="js/app.js"></script>
    <script type="text/javascript">
    document.addEventListener("deviceready", function() {
        cordova.plugins.diagnostic.isGpsLocationEnabled(function(enabled){
            console.log("GPS location is " + (enabled ? "enabled" : "disabled"));
            alert('hello');
        }, function(error){
            console.error("The following error occurred: "+error);
        })
    }, false);
    </script>
  </head>
  <body ng-app="starter">
    <ion-pane>
      <ion-header-bar class="bar-stable">
        <h1 class="title">Ionic Blank Starter</h1>
      </ion-header-bar>
      <ion-content>
      </ion-content>
    </ion-pane>
  </body>
</html>

1 个答案:

答案 0 :(得分:0)

可能是因为设备没有准备好而出现问题。尝试在deviceready()生命周期事件中添加代码。像这样:

document.addEventListener("deviceready", function() {
    cordova.plugins.diagnostic.isGpsLocationEnabled(function(enabled){
        console.log("GPS location is " + (enabled ? "enabled" : "disabled"));
    }, function(error){
        console.error("The following error occurred: "+error);
    })
}, false);