Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 从提供者获取数据_Angular - Fatal编程技术网

Angular 从提供者获取数据

Angular 从提供者获取数据,angular,Angular,我需要在组件中控制台数据,从提供者获取数据。当im使用subscribe TypeError时显示此错误:无法读取未定义的属性“subscribe” 我在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

我需要在组件中控制台数据,从提供者获取数据。当im使用subscribe TypeError时显示此错误:无法读取未定义的属性“subscribe”

我在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);
  }
public getSamad(公司id:any){
this.company\u id=company\u id;
this.url='url.com?offset=0&limit=10&company\u id='+this.company\u id;
this.clientData=this.httpClient.get(this.url)。
订阅(数据=>{
控制台日志(数据);
this.spinner.hide();
this.data1=data.records;
this.data1.forEach(d=>this.policy_id.add(d.policy_id));
console.log(this.userFilter.policy\u id);
}

我想在home.html中显示数据。任何人都可以告诉我如何在home.ts控制台中显示提供者的数据,谢谢

您需要在
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);
}
public getSamad(公司id:any){
this.company\u id=company\u id;
this.url='url.com?offset=0&limit=10&company\u id='+this.company\u id;
返回this.httpClient.get(this.url);
}

然后你可以在你的组件中订阅它,把订阅放在你的
getSamad
订阅中的东西里面。

你必须返回可观察的。例如:
返回这个.httpClient.get…
另外,你想要这样的管道:
返回这个.httpClient.get(…).pipe(map(你的回调方法=>)
如果您没有从api方法返回一个observable,您就无法订阅它。您能在代码方面提供帮助吗?我将替换为仍然不工作您的问题中没有足够的代码,我无法发布完整答案,但我将在JSFIDLE上发布一些代码供您尝试:是的,我以相同的方式尝试,但仍然显示subscribe i的相同错误如果没有定义,你可以发布一个可重复使用的示例。stackblitz就可以了。