angularjs - 等待订阅角度
问题描述
我需要等待订阅,然后将该结果分配给角度 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) 请为此问题提供答案
解决方案
const stocks = await this.stockService.getStocks(id).toPromise();
// DO STUFF this.data = stocks.data;
推荐阅读
- mysql - 为什么给定的查询会给出意外的输出?
- javascript - 如何发送带有特定数据的预定电子邮件,而不必在反应组件中呈现它?
- java - 找不到适合 jdbc:mariadb://127.0.0.1:3306/mydb?user=root 的驱动程序
- python - Numpy:np.repeat 和 np.broadcast_to 之间的区别
- javascript - React 钩子状态对于几个相等的事件有不同的值
- mapbox-gl-js - 簇几何与原始几何不同(有偏移,但为什么?)
- directus - 我无法更新输入值 Directus
- c++ - C++ 原始指针和“删除”
- esxi - ESXI 合并两个服务器来共享它们的资源?
- react-native - 使用 Wix React Native Navigation 关闭模式并更改选项卡