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

How to stop functions when leaving the page in Ionic 4

我正在我的 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 的任何错误时,加载程序保持 运行 并且当我移动到另一个页面时, 在那里显示。

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

非常感谢任何帮助。

好吧,我不知道停止你的功能的功能,但是当你离开页面时做一些事情,你在 IonViewDidLeave()

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

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

替换:const loading 与:

this.loading

现在在 ionViewWillLeave 调用中:

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