我正在使用带有promise的Loading Controller,但是某些加载程序会关闭,但有时在Ionic 4中不会关闭。 如何在ionic 4中使用可观察对象或行为对象的加载控制器?
我尝试了Internet上的各种链接,但问题是它有时不会消失。
我的代码在这里: Loaderservice.ts
isLoading = false;
constructor(private loadingCtrl:LoadingController) {}
async present(){
this.isLoading = true;
return await this.loadingCtrl.create({}).then(a=>{
a.present().then(()=>{
if(!this.isLoading){
a.dismiss().then(()=>console.log('abort presenting'));
}
});
});
}
async dismiss(){
this.isLoading = false;
return await this.loadingCtrl.dismiss().then(()=> console.log('dismissed'));
}
homepage.ts
getHomePage() {
this.loadingService.present();
let variationLatestProductsPriceArray = [];
this.homepageService.getHomePageData().subscribe(response => {
this.homePageModel = response['data'];
let checkAddress = this.homePageModel["address"];
if (checkAddress == true){
console.log("address Saved");
localStorage.setItem('newUser', 'true');
} else {
console.log("address not Saved");
localStorage.setItem('newUser', 'false');
}
console.log("latestProducts", this.latestProducts);
this.bannerList = this.homePageModel["slider"];
this.loadingService.dismiss();
}, (err) => {
console.log(err);
this.alert.myAlertMethod("OOPS ! NO INTERNET Please check your network connection.", err.error.message, data => {
console.log("hello alert")
});
this.loadingService.dismiss();
},()=>{
this.loadingService.dismiss();
});
}
有时它可以正常工作并正确将其关闭,但有时无法关闭。 我最近5天都在调查这个问题。
答案 0 :(得分:1)
重写当前功能:
async present(){
return await this.loadingCtrl.create({});
}
修改getHomePage函数,例如:
getHomePage() {
this.loadingService.present().then(event => {
event.present();
-----
event.dismiss();
})
}
我认为您会有所帮助。