【发布时间】:2016-12-22 01:01:01
【问题描述】:
我正在尝试在this website 的帮助下检测我的 wifi 连接是否已连接。我试过了:
export class HomePage {
constructor(public navCtrl: NavController, private platform: Platform) {
}
obtainNetworkConnection() {
console.log("hello");
// 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
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(() => {
if (Network.connection === 'wifi') {
console.log('we got a wifi connection, woohoo!');
}
}, 3000);
});
// stop connect watch
connectSubscription.unsubscribe();
}
}
我的 .html
<ion-content class="home" padding>
<button (click)="obtainNetworkConnection()" full>Get Nettwork Connection</button>
</ion-content>
【问题讨论】:
-
Emm.. 你把你的代码放在类体中,不要那样做 :)
-
把它放到方法中或者我猜你需要把它放到构造函数中
-
那么甲酸盐是什么帮助我,我在这条线上遇到了错误
let disconnectSubscription = Network.onDisconnect().subscribe(() => { -
类有方法,它不是函数。只需将此代码放入构造函数块中
-
是的,谢谢,我现在没有收到任何错误,但是...!!!如何从 UI 调用我的构造函数,以便我可以打印我的 console.log
标签: angular ionic2 typescript1.8