Angular无法使用可观察的响应形式

时间:2019-10-03 15:14:38

标签: angular

我已经为简单的get api调用创建了消费者。 服务:

  getTrendData(): Observable<AllVulnTrendData[]> {
    return this.http.get<AllVulnTrendData[]>(environment.backend + '/dashboard/getvulntrenddata')
      .pipe(
        retry(1),
        catchError(this.errorHandl),
      );
  }

组件:

trendResponse: AllVulnTrendData[];
loadTrendData() {
    this.dashboardService.getTrendData().subscribe(data => {
      this.trendResponse = data;
      console.log(JSON.stringify(this.trendResponse); //#1
    });

  }

我的用法是在构造函数内部:

constructor(private theme: NbThemeService, private httpService: DashboardChartService,
              private toastrService: NbToastrService, private dashboardService: DashboardService) {
    this.loadTrendData();
    console.log(JSON.stringify(this.trendResponse) //#2
  }

第1条日志包含http get响应,而第二条日志则没有。如何在可观察的方面使用加载的数据?

0 个答案:

没有答案
相关问题