Ionic Network 插件在打开 Wifi 之前无法识别连接 on/off

Ionic Network plugin doesn't identify connection until Wifi turned on/off

我的应用程序在 Ionic 3 上,我安装了 Network Plugin

我把它放在我的 Login.ts 上,然后像这样:

login.ts

private SUB_networkConnect: any;
private SUB_networkDisconnect: any;
private HAS_CONNECTION: boolean;

constructor( private network: Network ){
    this.SUB_networkConnect = this.network.onConnect().subscribe(() => {
        setTimeout(() => {
            this.HAS_CONNECTION = true;
            console.log(this.HAS_CONECTION);
        }, 3000);
    });

    this.SUB_networkDisconnect = this.network.onDisconnect().subscribe(() => {
        this.HAS_CONNECTION = false;
        console.log(this.HAS_CONECTION);
    });
}

我使用这个,因为第一次(有史以来)登录必须为用户下载资产和其他东西。

如果我没有连接,我不允许用户身份验证。

因此,当我关闭然后打开 Wifi 时,它开始触发控制台并且我无法进行身份验证,因为标志 HAS_CONNECTION 是在让我继续或不继续的条件下使用的

你们知道怎么处理吗?

我也试过app.component.ts,也没成功

@Rafael de Castro,如我所见,您的问题不在登录中,而是检测连接存在(是否有 Wifi)。

但是在您的代码中,您没有检查它是否已连接,而是等待一个事件来确定它是否在您的变量中有连接 this.HAS_CONNECTION!

试着把这条线

this.HAS_CONNECTION = (this.network.type === 'wifi' || this.network.type === '3g' || this.network.type === '4g');