首页 > 解决方案 > 数组在函数内部获取搜索结果的值,但似乎没有保留它

问题描述

我正在使用 Algolia 构建搜索功能。该函数成功检索了所需的数据,但我正在努力将其读入数组。考虑函数:

searchQ() {
 this.loading = true ;

 this.index.search({
   query: this.searchQuery
 },
 async function searchDone(err, content) {
    if (err) throw err;
    this.SearchResults = content.hits ;  
    console.log(this.SearchResults) ;
    console.log('hello2');
    this.loading = false ;
  }
 );

 this.searchActive = true ;
 console.log(this.SearchResults) ;
 console.log('hello1');
 this.changeRef.detectChanges();
}

请参阅控制台输出的附加图像。在此处输入图像描述

在搜索功能的第二次运行中,显示相同的输出。与 'hello1' 关联的数组从不接受 searchDone 函数之外的任何值。

此处声明的搜索结果:

export class Tab2Page {

  public SearchResults = [] ;
  ...

  constructor(...) {

  }
}

标签: angulartypescriptionic4

解决方案


推荐阅读