api - 等待 Localstorage 然后调用 api
问题描述
我从用户输入中获取设置并将其存储在本地存储中。我在 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调用发生在localstorage获取数据之前的问题。我怎样才能让 api 调用等待 localstorage
解决方案
使用 async 和 await 你可以解决这个问题。
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)
}
推荐阅读
- smtp - Splunk sendemail 使用邮件服务器 smtp.gmail.com 失败 [Errno 99]
- php - 如何优化限制查询以更快地从大表中访问数据?
- python - 如何打印包含单独列表总和的列表
- wpf - 尝试显示 MahApps Metro 对话框时出现异常
- excel - 如何运行我针对我的 excel 表的所有行编写的 vba?
- python - 以向量化方式计算数组切片上的函数
- javascript - EJS 应用程序上的搜索栏
- javascript - 反应:设置 div 的 scrollTop 属性不起作用
- python-requests - CRAWL QUESTION:关于请求的问题
- pandas - 尝试从 OECD API 获取多个指标时出现关键错误: