我如何检查 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/