在Ionic 4中离开页面时如何停止功能

时间:2019-06-14 07:39:15

标签: ionic-framework ionic4

我正在使用Ionic 4应用程序,我想在页面离开时停止功能。

这是我的 tab4.page.ts

async getUserDetail(){ 
    this.dataexists = false;
    this.userActiveChallanges = [];   
    let me=this;   
    const loading = await this.loadingController.create({
      message: '',
      // duration: 2200,
      translucent: true,
      spinner: 'crescent',
      showBackdrop: false,
      cssClass: 'my-loading-class'
    });
    await loading.present();  
    this.userActiveChallanges=[];
    this.storage.get('USERPROFILE').then(userObj => { 
      // console.log('User Profile :',userObj);
      me.userprofile = userObj; 
      me.sendFitDatafunction(userObj);
      me.myapi.apiCall('userActiveChallenges/'+userObj.id,'GET','').subscribe((data) => {
        // console.log(data); 
        me.response=data;       
        loading.dismiss();
        if(me.response.status === 'success'){
          if(me.response && me.response.data && me.response.data.length>0){
            this.userActiveChallanges=me.response.data;
            this.flip(this.userActiveChallanges[0].challenge_id);
          }
          this.dataexists = true;
        } else{
          this.userActiveChallanges = '';
          this.dataexists = true;
        }

      }, error => { loading.dismiss(); console.log(error); });

    });
  }

  ionViewWillLeave() {

  }

我要在页面离开时停止此功能,因为当我没有收到任何响应,也没有从api得到任何错误时,加载程序会继续运行,而当我移至另一页面时,它将显示在那里。

所以,我想在页面离开时停止该功能。

非常感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

将其声明为ts类(tab4)的属性,而不是本地const加载。

现在更改您的代码并为其分配加载程序:

替换:const loading

this.loading

现在在ionViewWillLeave调用中:

ionViewWillLeave() {
    if (this.loading) { this.loading.dismiss() }
} 

答案 1 :(得分:0)

好吧,我不知道该函数会停止您的函数,但是要在离开页面时生成某些东西,可以在IonViewDidLeave()中创建它