【发布时间】:2019-07-02 22:54:43
【问题描述】:
我正在从事 ionic 4 项目。我的项目正在从 url 获取数据 json 。我尝试使用Network native 进行离子检查我的应用程序的互联网连接。但我没有什么可显示的。没有数据,也没有要显示的警报。
constructor(private http: HTTP, public loadingController: LoadingController,private network: Network,
public alertController : AlertController, public platform : Platform) {
// watch network for a disconnection
let disconnectSubscription = this.network.onDisconnect().subscribe(() => {
this.AlertNet()
});
// stop disconnect watch
disconnectSubscription.unsubscribe();
// watch network for a connection
let connectSubscription = this.network.onConnect().subscribe(() => {
console.log('network connected!');
this.getData()
setTimeout(() => {
if (this.network.type === 'wifi') {
console.log('we got a wifi connection, woohoo!');
}
}, 3000);
});
// stop connect watch
connectSubscription.unsubscribe();
}
async getData() {
const loading = await this.loadingController.create({
message: 'Loading'
});
await loading.present();
this.http.get('/v1/airport.json?code=krt', {}, {})
.then(data => {
this.test = JSON.parse(data.data);
const parsed = JSON.parse(data.data);
}), err=>{
this.test =err
loading.dismiss()
}
}
async AlertNet(){
const alert = await this.alertController.create({
header: 'Alert',
subHeader: 'No internet',
message: 'You do not have an Internet connection. Please check your connection status',
buttons: [{
text: "Ok",
handler: () => { this.platform.backButton.subscribe(()=>{
navigator['app'].exitApp();
});
}
}]
});
await alert.present();
}
【问题讨论】:
-
你链接了文档,你关注了吗?您似乎没有在任何地方订阅连接?
it doesn't work也没有告诉我们任何事情。您是否收到错误消息或其他信息? -
没有什么可显示的! .也许我以错误的方式编写了代码! .你有其他选择吗?
-
你是按照文档说的那样做的吗?我不知道你期望
this.network.onConnect和this.network.onDisconnect应该做什么? -
我关注文档。我有很多错误
-
好吧,您显示的这段代码根本不是文档所说的。那么,如果您按照文档中的方式进行操作,您会得到什么错误? :)