Angular 我不知道';我不明白为什么它不';Don’不要进入for循环,有什么想法吗?
我不明白为什么它没有进入for循环,有什么想法吗Angular 我不知道';我不明白为什么它不';Don’不要进入for循环,有什么想法吗?,angular,typescript,for-loop,Angular,Typescript,For Loop,我不明白为什么它没有进入for循环,有什么想法吗 async getAllApis() { const apis: Api[] = []; console.log( this.all_api_id ); /* Output on console (correct): 0: "aura-insight-events"
async getAllApis() {
const apis: Api[] = [];
console.log( this.all_api_id ); /* Output on console (correct):
0: "aura-insight-events"
1: "audit"
2: "consent-events"
3: "backoffice"
*/
console.log( typeof(this.all_api_id) ); # Output on console (correct): Object
for await ( const id of this.all_api_id ) {
console.log( id ); # No answer ¿?
this.getAPIbyID( 'apis', id ).subscribe( ( api: Api ) => {
console.log( api ); # No answer (obviously)
apis.push( api[0] );
});
}
console.log( apis ); # Output on console: [] => length: 0
this.apis = apis;
}
我试过几种方法,但都没有效果。。我不明白为什么代码是这样的——如果有人明白,请告诉我为什么for循环线上的
wait
?这是一个字符串数组,您正在迭代,对吗?我看不出wait在这里还有其他用途。您还使用了带有subscribe的observable。Subscribe不适用于async/await关键字,async/await用于承诺
,而不是可观察的
。请参见上述内容。异步调用是编写/设计应用程序时常见且关键的构造块。理解如何使用javascript中的异步调用以及扩展typescript非常重要。理解这些核心概念将有助于你成为一名更好的程序员,也可以确保你不会在同一个问题上“伤脑筋”。