等待本地存储,然后调用api

时间:2019-05-30 04:39:45

标签: api ionic-framework angular6 local-storage ionic4

我从用户输入中获取设置,并通过本地存储进行存储。我在API调用中使用了这些数据。

 userlocation;
 userservice ;
 usercategory ;

 constructor(public http: HttpClient, public storage : Storage) { }

 getListings(){

 this.storage.get('area').then((userlocation) => {
  this.userlocation =  userlocation;

  console.log(this.userlocation)
 });

  this.storage.get('ser').then((userservice) => {
  this.userservice =  userservice;

  console.log(this.userlocation)
 });

  return  this.http.get(this.api_url+"?location="+ this.userlocation+ 
  "&ser= " + this.userservice)

 }

api调用在本地存储获取数据之前发生的问题。 如何使api调用等待本地存储

1 个答案:

答案 0 :(得分:0)

使用异步并等待您可以解决此问题。

userlocation;
 userservice ;
 usercategory ;

 constructor(public http: HttpClient, public storage : Storage) { }

 async getListings(){ 

 await this.storage.get('area').then((userlocation) => {
  this.userlocation =  userlocation;

  console.log(this.userlocation);

    await this.storage.get('ser').then((userservice) => {
      this.userservice =  userservice;

      console.log(this.userlocation)
    });
 });



  return  this.http.get(this.api_url+"?location="+ this.userlocation+ 
  "&ser= " + this.userservice)

 }
相关问题