在我的home.ts文件中,如果Internet连接处于脱机状态,我想使用localStorage项目。这是代码。当我在这里添加IF时,我有太多错误。如果互联网离线,我想在代码中将以下getItem用于“ __mydb / _ionickv / DodNalog”。
getStorageWarents() {
this.storage.ready()
.then(() => {
this.warrentsInStorage = JSON.parse(localStorage.getItem('__mydb/_ionickv/DodNalog'));
console.log("Warrents in storage: ", this.warrentsInStorage);
})
.then(() => {
this.getAssignedWarrents();
})
.then(() => {
this.getAllGoods();
})
.then(() => {
this.getAllCancelTypes();
})
.then(() => {
this.getAllDeviceTypes();
})
.then(() => {
this.getAllManufacturers();
})
.then(() => {
this.getAllIntereventionTypes();
})
}
答案 0 :(得分:0)
您可以像这样检查在线和离线事件
window.addEventListener("online", doSomthing());
window.addEventListener("offline", doSomthing());
答案 1 :(得分:0)
要在“连接/断开连接”上获取网络连接更新,可以在应用程序中使用ionic Network插件。
Network Plugin
安装:
ionic cordova plugin add cordova-plugin-network-information
npm install @ionic-native/network
如何使用?
import { Network } from '@ionic-native/network/ngx';
constructor(private network: Network) { }
订阅Connect / Disconnect事件:
let disconnectSub = this.network.onDisconnect().subscribe(() => {
console.log('device network was disconnected');
});
let connectSub = this.network.onConnect().subscribe(() => {
console.log('Device 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('we got a connection, woohoo!');
}, 3000);
});
要停止捕获上述事件,只需取消订阅
disconnectSub.unsubscribe();
希望这将有助于获取网络的连接/断开状态。