简体   繁体   English

有什么方法可以在离子应用程序中实现再试一次按钮吗?

[英]Is there any way to implement try again button into ionic app?

I want to create an app that has an alert for check connection with two button one is exit for the exit app and two is try again for check connection again, I searched about it and I tried about it, but I can n't solved this problem please help me.我想创建一个应用程序,它带有两个按钮检查连接的警报,一个是退出应用程序的退出,两个是再次尝试再次检查连接,我搜索了它并尝试了它,但我无法解决这个问题请帮帮我。

import { Injectable } from '@angular/core';
import { Network } from '@ionic-native/network/ngx';
import { AlertController } from '@ionic/angular';

@Injectable({
  providedIn: 'root'
})
export class CheckInternetService {

  public base: string; // this will be set in the constructor based on if we're in dev or prod
  timer: any;

  constructor(private network: Network, private alertCtrl: AlertController) {}

  async presentAlert() {
    const alert = await this.alertCtrl.create({
      header: 'خطا',
      backdropDismiss: false,
      subHeader: 'قطعی انترنت',
      message: 'لطفا انترنت خودرا چک کنید',
      buttons: [{
        text: 'خروج',
        handler: () => {
          navigator['app'].exitApp();
        }
      },
      {
        text: 'تلاش مجدد',
        handler: () => {
          this.doSomething().then(res => {
            this.checkConnection();
          });
        }
      }
    ],
    });
    await alert.present();
  }

  doSomething() {
    return new Promise((resolve, reject) => {
      // pretend a long-running task
      this.timer = setTimeout(() => { resolve(true); }, 3000);
    });
  }

  checkConnection(): boolean {

    if (document.URL.includes('https://') || document.URL.includes('http://')) {
      this.base = 'http://127.0.0.1:3001/';
    } else {
      this.base = 'https://url.to_actual_URL.com/';
    }
    const type = this.network.type;

    let online;

    if (type === 'unknown' || type === 'none' || type === undefined) {
      online = false;
      this.presentAlert();
    } else {
      online = true;
      clearTimeout(this.timer);
    }

    this.network.onDisconnect().subscribe( () => {
      online = false;
      this.presentAlert();
    });

    this.network.onConnect().subscribe( () => {
      online = true;
      clearTimeout(this.timer);
    });

    return online;
  }
}

This is my code that I was trying on, I work on this code but I do n't any answer, please help me.这是我正在尝试的代码,我正在处理此代码但我没有任何答案,请帮助我。

You can make try again button with out any timer, you can use this code for your problem:您可以在没有任何计时器的情况下重试按钮,您可以使用此代码解决您的问题:

  async presentAlert() {
    this.alertCtrl.dismiss();
    const alert = await this.alertCtrl.create({
      header: 'خطا',
      backdropDismiss: false,
      subHeader: 'قطعی انترنت',
      message: 'لطفا انترنت خودرا چک کنید',
      buttons: [{
        text: 'خروج',
        handler: () => {
          navigator['app'].exitApp();
        }
      },
      {
        text: 'تلاش مجدد',
        // role: 'cancel',
        handler: () => {
          // this.doSomething().then(res => {

          // this.checkConnection();
          // });
          const type = this.network.type;

          if (type === 'unknown' || type === 'none' || type === undefined) {
            this.presentAlert();
          }

        },
      }
    ],
    });
    await alert.present();
  }

I don't think you can have a alert box with two buttons because Alert box gives only one button "OK" to select and proceed.我不认为你可以有一个带有两个按钮的警报框,因为警报框只提供一个按钮“确定”来选择和继续。 . . You can show a modal instead of a alert box with as much button as you want.您可以使用任意数量的按钮显示模式而不是警告框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM