【问题标题】:Redirecting to other page when alert message clicked OK in an Ionic App当 Ionic 应用程序中的警报消息单击确定时重定向到其他页面
【发布时间】:2021-01-07 20:32:07
【问题描述】:

我是 Ionic 的新手,正在开发 Ionic 应用程序。当用户设备位置关闭时,我确实在页面中显示了警报消息。此警报消息工作正常。现在我的要求是在警报消息单击确定时将应用程序重定向到不同的页面(home.page.html)。我用 return this.homePage 但它不起作用。我想我需要编写一些函数来将其重定向到家。下面是我的代码。

location.ts 页面:

ngOnInit() {
this.helperService.showLoader("Fetching your location");
this.mapElement = this.mapElement.nativeElement;
this.getUserLocation()
  .then((res) => {
    this.myLocation = res;
    this.helperService.hideLoader();
    return this.myLocation;
  })
  .catch((error) => {
      console.log('Error getting location', error);
      this.helperService.hideLoader();

      alert(
        "Location setting should be ON to use this feature, please turn it ON in the Location Settings on your phone "
      );

      return this.homePage;
  })

请帮助我如何做到这一点。谢谢

【问题讨论】:

    标签: angular ionic-framework


    【解决方案1】:

    您应该使用 Ionic 警报而不是 JavaScript 警报。这将为您提供回调

    我在下面添加了示例代码,请使用这个。我在示例代码中添加了两个ok and cancelbutton 如果你想要其中一个,你可以从数组中删除它

    //导入声明

    import { AlertController } from '@ionic/angular';
    

    //构造函数

    constructor(private alertController : AlertController) {
        
    }
    

    在你的页面类中添加这两个函数

    ngOnInit() {
        this.helperService.showLoader("Fetching your location");
        this.mapElement = this.mapElement.nativeElement;
        this.getUserLocation()
          .then((res) => {
            this.myLocation = res;
            this.helperService.hideLoader();
            return this.myLocation;
          })
          .catch((error) => {
            console.log('Error getting location', error);
            this.helperService.hideLoader();
    
            this.presentAlertConfirm(
              "Location setting should be ON to use this feature, please turn it ON in the Location Settings on your phone "
            );
    
          })
      }
    
      async presentAlertConfirm() {
        const alert = await this.alertController.create({
          cssClass: 'my-custom-class',
          header: 'application name',
          message: 'Location setting should be ON to use this feature, please turn it ON in the Location Settings on your phone',
          buttons: [
            {
              text: 'Cancel',
              role: 'cancel',
              cssClass: 'secondary',
              handler: (blah) => {
                console.log('Confirm Cancel: blah');
              }
            }, {
              text: 'Okay',
              handler: () => {
                console.log('Confirm Okay');
                 //you can write your code or redirection 
                 // sample redirection code 
                 // this.router.navigate(['homepage'])
              }
            }
          ]
        });
    
        await alert.present();
      }
    

    【讨论】:

    • 嗨@Taylor Rahul,谢谢你的回答。当我应用您的代码时,我收到声明错误。我是这个离子的新手,我不明白如何处理这些东西。是否可以重定向到仅保留此 javascript 警报的其他页面。请帮忙。谢谢
    • 你能告诉我你得到什么声明错误吗..你可以更新你的问题。我们一起来解决.. JavaScript 不提供这个回调,所以它会给你带来问题。
    • 我已经更新了我的答案并添加了几乎所有的声明。请立即查看
    • @Taylor Rahul,出现错误找不到名称“presentAlertConfirm”。在页面中添加的其他警报中也会出现错误。如果继续在聊天中讨论会很棒,我会准确地分享问题。
    • 对不起,我忘了在函数名之前添加这个。我再次更新了我的代码。请使用更新的。如果您仍然有任何错误,请告诉我
    【解决方案2】:

    您可以使用 NavController 将应用程序导航到主页。

    离子 V3:https://ionicframework.com/docs/v3/api/navigation/NavController/

    Ionic V4+ : this.navCtrl.navigateRoot('/home');

    使用烤面包机控制器代替 alert()

    https://ionicframework.com/docs/api/toast

    async ngOnInit() {
     this.helperService.showLoader("Fetching your location");
     this.mapElement = this.mapElement.nativeElement;
     this.getUserLocation()
      .then((res) => {
        this.myLocation = res;
        this.helperService.hideLoader();
        return this.myLocation;
      })
      .catch((error) => {
        console.log('Error getting location', error);
        this.helperService.hideLoader();
    
         const toast = await this.toastController.create({
         message: 'Location setting should be ON to use this feature, please turn it ON in the Location Settings on your phone ',
         position: 'top',
         buttons: [
           {
             text: 'cancel',
             handler: () => {
               console.log('Cancel clicked');
             }
            }, {
             text: 'Done',
             role: 'cancel',
             handler: () => {
               this.navCtrl.navigateRoot('/home');
             }
           }
        ]
      });
      toast.present();
    

    })

    您可以根据需要添加按钮

    【讨论】:

    • 嗨@Hitesh Patel,非常感谢您的回答。我不知道当我应用您的代码时,位置关闭功能无法正常工作,也不会显示警报。是否可以仅使用 javascript 警报进行重定向。请帮我。谢谢
    猜你喜欢
    • 2018-07-29
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多