angular - 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 控制台中显示提供者的数据有多糟糕谢谢
解决方案
您需要在您的方法中返回 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
。
推荐阅读
- swift - SwiftUI:您能否为数据本身的变化制作动画,而不仅仅是视图对变化的响应?
- python - 理解其中一种python数据类型有问题
- c# - 通过拆分从多个文本文件或一个文件中搜索网站上出现的特定单词
- azure-cosmosdb - 唯一 ID 是 CosmosDB 的最佳分区键吗
- docker - Portainer:在尝试连接到 Docker 守护进程套接字时获得权限被拒绝
- python - Python Devtools 阻止 selenium click()
- javascript - canvas.fillRect 不在浏览器窗口上显示矩形
- javascript - WebDriverIO - 在 Google Chrome 上运行时出现奇怪的行为
- javascript - 我收到“Firebase 存储:`put` 中的参数计数无效:预期在 1 到 2 个参数之间,收到 0
- c++ - 二叉搜索树中元素的下限值