我想输入的这种代码是用户是否连接到网络。如果已连接以执行此操作,则将阻塞,如果未连接则执行其他操作,例如从本地存储获取数据。我很困惑该怎么做。
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)
您可以使用 navigator
全局对象。
getStorageWarents() {
isOnline: boolean = navigator.onLine;
if(isOnline){
// code block
} else {
// get data from local storage
}
}
请遵循此article
您还可以使用 ng-connection-service
import { ConnectionService } from 'ng-connection-service';
isOnline:boolean;
isConnected = true;
constructor(private connectionService: ConnectionService) {
this.connectionService.monitor().subscribe(isConnected => {
this.isConnected = isConnected;
if (this.isConnected) {
this.isOnline = true;
}
else {
this.isOnline = false;
}
})
}
getStorageWarents() {
if(this.isOnline){
// code block
} else {
// get data from local storage
}
}