Ionic 2 Cordova 网络信息插件引用错误
Ionic 2 Cordova network information plugin reference error
我已经按照本教程将网络信息插件安装到我的 Ionic 2 应用程序中:
Ionic 2 | How to Work With Cordova Plugins
但是 TypeScript 无法编译,因为它无法在 states 数组行中找到 "Connection" 的引用。知道如何根据平台、页面等为其编写导入语句吗?
我的class:
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'.
注意:插件不能necessarily/always在浏览器中运行,因此请使用真实设备或模拟器。
修复 TypeScript 错误:
- 在 /app
中创建名为 Typings 的文件夹
- 将此文件下载到该文件夹中:phonegaptypings here
在文件夹中创建另一个文件,例如ngcordova.d.ts 使用此代码:
界面Window{
插件:任何;
}
然后修改你的tsconfig.jsonfiles
属性:
"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
您应该在导入行下添加以下内容
declare var navigator: any;
declare var Connection: any;
您是否在目标设备上尝试过运行?许多插件无法在浏览器中运行(开箱即用)。
我已经按照本教程将网络信息插件安装到我的 Ionic 2 应用程序中:
Ionic 2 | How to Work With Cordova Plugins
但是 TypeScript 无法编译,因为它无法在 states 数组行中找到 "Connection" 的引用。知道如何根据平台、页面等为其编写导入语句吗?
我的class:
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'.
注意:插件不能necessarily/always在浏览器中运行,因此请使用真实设备或模拟器。
修复 TypeScript 错误:
- 在 /app 中创建名为 Typings 的文件夹
- 将此文件下载到该文件夹中:phonegaptypings here
在文件夹中创建另一个文件,例如ngcordova.d.ts 使用此代码:
界面Window{ 插件:任何; }
然后修改你的tsconfig.jsonfiles
属性:
"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
您应该在导入行下添加以下内容
declare var navigator: any;
declare var Connection: any;
您是否在目标设备上尝试过运行?许多插件无法在浏览器中运行(开箱即用)。