如何处理异步离子本机HTTP.get / .post

问题描述

我知道这是一个异步问题,它是如何工作的,但是到目前为止,我发现的唯一解决方案是settimeout

MyService.ts

import {  GlobalService } from '../services/global.service';

....
..

getContent()
{
 this.token=this.globalService.Token();
 this.ID=this.globalService.ID();
 this.nativeHttp.setDataSerializer('json');
  this.nativeHttp.post(apiurl.apiUrl+'page/myPage',{
    "Authentication_Token":this.token,"Id":this.ID,},{
 'Accept': 'application/json','Content-Type': 'application/json' })
 .then(response => {
   try {
     response.data = JSON.parse(response.data);
     this.data=response.data;
     this.storage.set('content',this.data);
   } catch(e) {
     console.error('JSON parsing error');
   }
})
.catch(response => {
  console.log(response.status + "error1");

  console.log(response.error + "error2");
});
}

MyPage.ts

import { HTTP } from '@ionic-native/http/ngx';


ShowContent(){
  this.MyService.getContent();
  this.storage.get('content').then((val) => {
    this.Show= val;
});
}

我是离子新手,已经玩了一段时间。 通常,为了解决此问题,我将包含一个setTimeout并祈祷数据库将设置 它将在代码“ storage.get”发生之前的5秒钟内放入离子存储中。

  this.MyService.getContent();
  setTimeout(() => {
  this.storage.get('content').then((val) => {
    this.Show= val;
   },5000);

我想知道是否有确定方法可以首先检测到内容,然后运行此行

this.storage.get('content').then((val) => {
    this.Show= val;

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)