首页 > 解决方案 > Angular 从提供者那里获取数据

问题描述

我需要控制台组件中的数据从提供者那里获取。当我使用订阅类型错误时显示此错误:无法读取未定义的属性“订阅”

我在 home.html 中这样获取

  this.api.getSamad(this.company_id).subscribe(data=> console.log (data));

提供者

public getSamad(company_id: any){
this.company_id = company_id;
this.url = 'url.com?offset=0&limit=10&company_id='+this.company_id;

 this.clientData = this.httpClient.get<any>(this.url).
 subscribe(data => {
 console.log(data);
         this.spinner.hide();

         this.data1 = data.records;
         this.data1.forEach(d => this.policy_id.add(d.policy_id));
         console.log(this.userFilter.policy_id);
  }

我想在 home.html 中显示数据,谁能告诉我在 home.ts 控制台中显示提供者的数据有多糟糕谢谢

标签: angular

解决方案


您需要在您的方法中返回 observable getSamad,并且不要在那里订阅它。您的方法没有返回任何内容,这就是它显示该错误的原因。

public getSamad(company_id: any){
  this.company_id = company_id;
  this.url = 'url.com?offset=0&limit=10&company_id='+this.company_id;
  return this.httpClient.get<any>(this.url);
}

然后你可以在你的组件中订阅它,把你订阅的内容放在那个订阅里面getSamad


推荐阅读