2016-02-26 75 views
2

我已經安裝了網絡信息每本教程插件作爲進入我的離子2應用程序:離子2科爾多瓦網絡信息插件引用錯誤

Ionic 2 | How to Work With Cordova Plugins

然而打字稿不能編譯,因爲它不能找到狀態數組行中的「連接」參考。任何想法如何爲平臺,頁面等編寫導入語句?

我的類:

import {NavController, NavParams} from 'ionic-framework/ionic'; 
import {Page, ViewController, Platform, Alert, Modal, Events} from 'ionic-framework/ionic'; 
import {forwardRef} from 'angular2/core'; 
import {OnInit} from 'angular2/core'; 
import {Injectable} from 'angular2/core'; 
import {TodosService} from '../../services/TodosService'; 
import {MyTodosItem} from '../../pages/my-todos-item/my-todos-item'; 

@Page({ 
    templateUrl: 'build/pages/my-todos/my-todos.html' 
}) 
class TodayPage { 

    constructor(
    private platform: Platform, 
    private nav: NavController, 
    private _events: Events, 
    private _todosService: TodosService) { 
    this._platform = platform; 
    this._isAndroid = platform.is('android'); 
    } 

    ngOnInit() { 
    this.getItems(); 
    this._events.subscribe('item:updated',() => { 
     this.getItems(); 
    }); 

    this.checkNetwork(); 
    } 

    checkNetwork() { 
     this._platform.ready().then(() => { 
      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(states[networkState]); 
     }); 
    } 
.. 
} 

錯誤是:
找不到名稱 '連接'。

回答

6

注:插件不一定/總是在瀏覽器中運行,因此使用真實設備或模擬器。

要解決打字稿錯誤:

  1. 創建/應用程序稱爲分型文件夾
  2. 將此文件下載到該文件夾​​:phonegaptypings here
  3. 創建的文件夾例如,另一個文件ngcordova.d.ts使用此代碼:

    接口窗口{plugins:any; }

然後修改tsconfig.json files屬性:

"files": [ 
    "app/app.ts", 
    "app/typings/ngcordova.d.ts", 
    "app/typings/phonegap.d.ts" 
    ] 

現在,打字稿不應該抱怨,它會建立,但重要:一個真實的設備或仿真器上測試,不是瀏覽器。

這爲我工作。

1

終於發現

溶液加入分型/ main.d.ts以下獲得網絡信息,才能正常工作

/// <reference path="ngcordova.d.ts" /> 

/// <reference path="phonegap.d.ts" /> 

這將會對角

1
最新版本工作

您應該在進口線下添加以下內容

declare var navigator: any; declare var Connection: any;

0

您是否嘗試過在目標設備上運行呢?許多插件在瀏覽器中無法使用(開箱即用)。