【问题标题】:Cannot read the property 'type' of undefined error in cordova network plugin无法读取科尔多瓦网络插件中未定义错误的属性“类型”
【发布时间】:2014-12-15 00:43:11
【问题描述】:

我正在使用 cordova 和 ionic 框架开发 android 应用程序,使用来自的网络插件 在这里(https://github.com/apache/cordova-plugin-network-information)。但是设备就绪警报触发,然后我收到以下错误。

"TypeError: Cannot read property 'type' of undefined

这里是导航器对象

navigator.connection.type 

下面一行会出错。

【问题讨论】:

    标签: javascript android cordova cordova-plugins


    【解决方案1】:

    由于某种原因,navigator.connection 对象没有像cordova一样同时准备好。

    您必须使用超时,例如这个 - 为 Angular/Ionic 设计的

       setTimeout(function(){
            var connection = connectionService.getConnection();
            console.log("connection : "+connection);
            $scope.connection = connection;
            $scope.$apply()
        }, 5000)
    

    【讨论】:

      【解决方案2】:

      用我的

      document.addEventListener("deviceready", onDeviceReady, false);
      // Cordova is ready
      function onDeviceReady() {
          checkConnection()
      }
      
      function checkConnection() {
          networkState = navigator.connection.type;
          var states = {};
          states[Connection.UNKNOWN] = 'Unknown connection';
          states[Connection.ETHERNET] = 'Ethernet connection';
          states[Connection.WIFI] = 'WiFi connection';
          states[Connection.CELL_2G] = 'Cell 2G connection';
          states[Connection.CELL_3G] = 'Cell 3G connection';
          states[Connection.CELL_4G] = 'Cell 4G connection';
          states[Connection.CELL] = 'Cell generic connection';
          states[Connection.NONE] = 'No network connection';
          alert('Connection type: ' + states[networkState]);
      }
      

      【讨论】:

        猜你喜欢
        • 2018-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多