首页 > 解决方案 > 等待订阅角度

问题描述

我需要等待订阅,然后将该结果分配给角度 7 中的特定变量,但该代码没有等待我的代码是这样的。

async getItemsbyId(id) {
console.log('2')
 await  this.stockService.getStocks(id).subscribe(
   (res: any) => {
   console.log('3')
    this.data = res.data;
    return this.data;
  },
  err => {
    console.log(err);
  },
  () => {
    this.http_item = null;
  }
);
console.log('4')
} 

get_value(){
console.log('1')
this.getItemsbyId(5);
console.log('5')
}

我需要按顺序控制台到 (1,2,3,4,5) 但我得到 (1,2,4,5,3) 请为此问题提供答案

标签: angularjsangularangular-material

解决方案


const stocks = await this.stockService.getStocks(id).toPromise();
// DO STUFF this.data = stocks.data;

推荐阅读