gpt4 book ai didi

ionic-framework - Ionic 4 网络检查

转载 作者:行者123 更新时间:2023-12-03 23:36:39 24 4
gpt4 key购买 nike

我如何检查 Ionic 4 中每个页面的互联网连接。如果出现网络错误,我需要将应用程序重定向到错误页面。

最佳答案

您可以尝试官方的 Ionic Network 插件(文档 here)。

来自文档的用法示例:

import { Network } from '@ionic-native/network/ngx'; //ngx

constructor(private network: Network) { }

...

// watch network for a disconnect
let disconnectSubscription = this.network.onDisconnect().subscribe(() => {
console.log('network was disconnected :-(');
});

// stop disconnect watch
disconnectSubscription.unsubscribe();


// watch network for a connection
let connectSubscription = this.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 (this.network.type === 'wifi') {
console.log('we got a wifi connection, woohoo!');
}
}, 3000);
});

// stop connect watch
connectSubscription.unsubscribe();

关于ionic-framework - Ionic 4 网络检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53064711/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com