Ionic 4: “Loading Controller” dismiss() is called before present() which will keep spinner without dismissing

前端 未结 20 2286
逝去的感伤
逝去的感伤 2020-12-08 01:06

I used \"Ionic Loading Controller\" to show a spinner until the data is retrieved then it calls \"dismiss()\" to dismissed it. it works fine, but sometimes when the app alre

20条回答
  •  猫巷女王i
    2020-12-08 01:24

    I'm using a similar solution but relying on the Ids of the loading overlays and letting the Ionic Loading Controller manage what overlay should be on top.

    LoadingService

    import { Injectable } from '@angular/core';
    import { LoadingController } from '@ionic/angular';
    
    @Injectable({
      providedIn: 'root'
    })
    export class LoadingService {
    
      constructor(public loadingController: LoadingController) { }
    
      async present(loadingId: string, loadingMessage: string) {
        const loading = await this.loadingController.create({
          id: loadingId,
          message: loadingMessage
        });
        return await loading.present();
      }
    
      async dismiss(loadingId: string) {
        return await this.loadingController.dismiss(null, null, loadingId);
      }
    }
    

    Components/Services using the LoadingService

    import { LoadingService } from '../loading/loading.service';
    
    @Injectable({
      providedIn: 'root'
    })
    export class MessagesService {
    
      ...
    
      constructor(
        protected http: HttpClient,
        protected loading: LoadingService
      ) { }
    
      ...
    
      protected async loadMessagesOverview() {
        const operationUrl = '/v1/messages/overview';
    
        await this.loading.present('messagesService.loadMessagesOverview', 'Loading messages...');
    
        this.http.get(environment.apiUrl + operationUrl)
          .subscribe((data: Result) => {
            ...
            this.loading.dismiss('messagesService.loadMessagesOverview');
          }, error => {
            ...
            this.loading.dismiss('messagesService.loadMessagesOverview');
            console.log('Error getting messages', error);
          });
      }
    
    }
    
    

提交回复
热议问题