[英]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.