Reference 爱奥尼亚2 Cordova网络信息插件参考错误
我已根据本教程将网络信息插件安装到我的Ionic 2应用程序中: 但是,TypeScript不会编译,因为它在states数组行中找不到“Connection”的引用。知道如何根据平台、页面等编写导入语句吗 我的班级:Reference 爱奥尼亚2 Cordova网络信息插件参考错误,reference,angular,cordova-plugins,ionic2,Reference,Angular,Cordova Plugins,Ionic2,我已根据本教程将网络信息插件安装到我的Ionic 2应用程序中: 但是,TypeScript不会编译,因为它在states数组行中找不到“Connection”的引用。知道如何根据平台、页面等编写导入语句吗 我的班级: import {NavController, NavParams} from 'ionic-framework/ionic'; import {Page, ViewController, Platform, Alert, Modal, Events} from 'ionic-f
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]);
});
}
..
}
错误是:找不到名称“Connection”。注意:插件不一定/总是在浏览器中工作,因此使用真实的设备或模拟器。 要修复TypeScript错误,请执行以下操作:
文件
属性:
"files": [
"app/app.ts",
"app/typings/ngcordova.d.ts",
"app/typings/phonegap.d.ts"
]
现在,TypeScript不应该抱怨,它将构建,但重要的是:在真实设备或模拟器上测试,而不是在浏览器上测试
这对我很有效。终于找到了解决办法 在typings/main.d.ts中添加以下内容,以使网络信息正常工作
/// <reference path="ngcordova.d.ts" />
/// <reference path="phonegap.d.ts" />
//
///
这将适用于angular的最新版本。您应该在导入行下添加以下内容
声明var导航器:任意;
声明var连接:任何代码>您是否尝试在目标设备上运行它?许多插件在浏览器中无法正常工作(开箱即用)