简体   繁体   English

使用Ionic2和Angular2的类型'Navigator'上不存在属性'connection'

[英]Property 'connection' does not exist on type 'Navigator' using Ionic2 and Angular2

I want to check my network using Ionic 2 and Angular 2. I receive a strange error that I cannot understand. 我想使用Ionic 2和Angular 2检查我的网络。我收到一个我无法理解的奇怪错误。

My app.ts is: 我的app.ts是:

import {App, Platform} from 'ionic-angular';
import {StatusBar} from 'ionic-native';
import {HomePage} from './pages/home/home';


@App({
  template: '<ion-nav [root]="rootPage"></ion-nav>',
  config: {} // http://ionicframework.com/docs/v2/api/config/Config/
})
export class MyApp {
  rootPage: any = HomePage;

  static get parameters() {
    return [[Platform]];
  }


  constructor(platform: any) {
    this.rootPage = HomePage;

    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      StatusBar.styleDefault();
    });
  }

}

My home.ts: 我的home.ts:

 import {App, Platform} from 'ionic-angular';
    import {StatusBar} from 'ionic-native';
    import {Network, Connection} from 'ionic-native';
    import {Page} from 'ionic-angular';



    @Page({
      templateUrl: 'build/pages/home/home.html',
    })

    export class HomePage {
      rootPage: any = HomePage;

      navigator = navigator;

      constructor(public platform: Platform) {
        this.platform = platform;
      }


      checkNetwork() {

        this.platform.ready().then(() => {
          // Problem1
          var 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]);

        });

      }

    }

My home.html 我的home.html

<ion-navbar *navbar>
    <ion-title>
        Home
    </ion-title>
</ion-navbar>

<ion-content class="home">
    <button (click)="checkNetwork()">Check Network</button>
</ion-content>

I receive one error and I totally not understand the reason: 我收到一个错误,但我完全不明白原因:

1 - Property 'connection' does not exist on type 'Navigator'

Update: 更新:

I went to Ionic 2 site and currently my home.ts file look like this: 我去了Ionic 2网站,目前我的home.ts文件如下所示:

import {Platform, Page} from 'ionic-angular';
import {Connection} from 'ionic-native';
import {Network} from 'ionic-native';

@Page({
  templateUrl: 'build/pages/home/home.html',
})

export class HomePage {
  rootPage: any = HomePage;

  constructor(public platform: Platform) {
    this.platform = platform;
  }


  checkConnection() {
    console.log("entrou");

    console.log(Network);

    // watch network for a disconnect
    let disconnectSubscription = Network.onDisconnect().subscribe(() => {
      console.log('network was disconnected :-( ')
    });

    // stop disconnect watch
    disconnectSubscription.unsubscribe();



    // watch network for a connection
    console.log("watch network");
    console.log("Conexao" + Network.connection);
    let connectSubscription = Network.onConnect().subscribe(() => {
      console.log('network connected!');
      // We just got a connection but we need to wait briefly
      // before we determine the connection type.  Might need to wait?
      // prior to doing any api requests as well.
      setTimeout(() => {
        console.log(Network.connection);
        if (Network.connection === Connection.WIFI) {
          console.log('we got a wifi connection, woohoo!');
        }
      }, 3000);
    });

    console.log("Sub" + connectSubscription);
    // stop connect watch
    connectSubscription.unsubscribe();
  }
}

I get a console message saying: 我收到一条控制台消息,说:

Native: tried accessing the Network plugin but Cordova is not available. 本机:尝试访问网络插件,但Cordova不可用。 Make sure to include cordova.js or run in a device/simulator 确保包含cordova.js或在设备/模拟器中运行

It seems like the Connection Plugin is not installed properly... 似乎连接插件未正确安装...

cordova plugin add cordova-plugin-network-information

also what is this line doing? 这条线还在做什么?

navigator = navigator;

Add TypeScript definition using npm npm install @types/cordova-plugin-network-information --save . 使用npm添加TypeScript定义npm install @types/cordova-plugin-network-information --save If it doesn't work, pls try using Network.type instead of navigator.connection.type 如果不起作用,请尝试使用Network.type而不是navigator.connection.type

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ionic2错误类型“窗口”上不存在属性“ openDatabase” - Ionic2 error Property 'openDatabase' does not exist on type 'Window' Ionic2 + Angular2-尝试分配给只读属性 - Ionic2 + Angular2 - Attempted to assign to readonly property 解决promise angular2:类型“ []”上不存在属性“ includes” - Resolving promise angular2: Property 'includes' does not exist on type '[]' 类型&#39;any []&#39;(JSON)上不存在Angular2属性 - Angular2 Property does not exist on type 'any[]' (JSON) Angular2和Typescript-类型上不存在属性名称 - Angular2 & Typescript - Property name does not exist on type “Navigator”类型上不存在属性“userAgentData” - Property 'userAgentData' does not exist on type 'Navigator' 如何在图像Angular2 Ionic2上绘制 - How to Draw on Image Angular2 Ionic2 Ionic2,Angular2,HTTP和Observables - Ionic2, Angular2, HTTP and Observables Angular 4 typescript Cordova Camera 插件错误“属性 &#39;camera&#39; 在类型 &#39;Navigator&#39; 上不存在” - Angular 4 typescript Cordova Camera plugin error "Property 'camera' does not exist on type 'Navigator'" Angular2 map - &gt; subscribe数据类型错误(类型&#39;ErrorObservable&#39;上不存在属性&#39;length&#39;。) - Angular2 map -> subscribe data type error ( Property 'length' does not exist on type 'ErrorObservable'.)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM