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

前端 未结 20 2246
逝去的感伤
逝去的感伤 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条回答
  •  执念已碎
    2020-12-08 01:21

    Using a list worked better for me

    import { Injectable } from '@angular/core';
    import { LoadingController } from '@ionic/angular';
    
    @Injectable({providedIn: 'root'})
    export class LoadingService {
        private loaders = new Array();
        constructor(public loadingController: LoadingController) { }
    
        present(options?: object) {
            if (this.loaders.length === 0) {
                this.loadingController.create(options).then(loader => {
                    this.loaders.push(loader);
                    loader.present();
                });
            }
        }
    
        async dismiss() {
            if (this.loaders && this.loaders.length > 0) {
                this.loaders.forEach(async loader => {
                    await loader.dismiss()
                        .then(() => {
                            loader = null;
                        })
                        .catch(e => console.log(e))
                        .finally(() => this.loaders = new Array());
                });
            }
        }
    }
    

提交回复
热议问题