我遇到了这个问题,我不知道如何对待async方法,因为ngOnChanges不支持async。
你们怎么做才能让这一切发生?
当我将async应用于ngOnChanges时,它不起作用。返回值为Promise。
@Component({
moduleId: module.id,
selector: 'search',
templateUrl: 'search.template.html'
})
export class Search implments OnChanges {
async queryArray(data: string): Promise<T> {
//sample scripts.
return ....;
}
ngOnChanges(changes: SimpleChanges) {
let query: string = "red";
let result: string[] = [];
await this.queryArray(query).then(resp => result = resp);
}}
发布于 2017-06-22 20:48:36
asyncawait以等待Promise的结果then,因为现在您可以在添加await后直接分配结果代码:
async ngOnChanges(changes: SimpleChanges) {
let query: string = "red";
let result: string[] = [];
result = await this.queryArray(query);
}还请注意,queryArray不需要标记为async,除非您也希望等待该方法的结果以进行进一步处理。如果它所做的只是创造和回报一个承诺,那么它就没有必要了。
https://stackoverflow.com/questions/44708997
复制相似问题