【发布时间】:2021-08-13 16:38:18
【问题描述】:
尝试使用 REST API 构建 woocommerce 应用
基本上,我使用 promise 将数据接收到变量中
我使用相同的变量将数据显示到前端 HTML
<h3 *ngIf = "onsale" style="text-align:center;font-weight: 800;margin-top:5px">Rs. {{productregprice}} / {{productonsaleprice}}</h3>
<h3 *ngIf = "!onsale" style="text-align:center;font-weight: 800;margin-top:5px">Rs. {{productonsaleprice}}</h3>
我面临的问题是 - 随着休息 API 调用完成,数据被加载到变量 productregprice 和 productonsaleprice 中
同时在前端 - HTML 视图被渲染,因此它给出了未定义的错误
我无法理解如何等待异步调用完成然后将数据提供给 HTML 标签以显示实际价格而不是 0
我的打字稿代码是
productregprice = 0;
productonsaleprice = 0;
onsale = true;
productshortdescription = "";
catids = this.route.getCurrentNavigation().extras.state.productid;
secondurl = `${this.url}/wp-json/wc/v3/products/${this.catids}?consumer_key=${this.key}&consumer_secret=${this.secretkey}`;
constructor(private http: HttpClient,private route: Router)
{
console.log(this.route.getCurrentNavigation().extras.state.productid);
this.product = this.getProductsData().then(res=>console.log(res));
console.log(this.product);
if(typeof this.product.images != 'undefined')
{
this.productimages = this.product.images;
}
else
{
this.productimages = [];
}
console.log("loaded");
}
getProductsData()
{
return new Promise(resolve => {this.http.get(this.secondurl).subscribe(productdata => {resolve(productdata);});});
}
请帮忙
【问题讨论】:
标签: angular typescript ionic-framework