api - ReferenceError:数据未在角度 6 中定义
问题描述
我通过 assignGirdle 函数从我的一个组件调用服务。在执行此服务时,我收到上述错误,但是当我检查网络选项卡并单击 API 调用时,响应中可以看到数据。 注意 girdleNew 是 any 类型。我也在 ngOnInit() 上调用这个函数
assignGirdle() {
this.diamondSearchService.getDistinctValues()
.subscribe((data) => {
this.girdleNew = data;
}, error => {
this.alertify.error(error);
});
}
服务:
getDistinctValues() {
return this.authHttp
.get(this.baseUrl + 'distinct/girdle')
.map(response => response.json())
.catch(this.handleError);
}
解决方案
检查您是否在组件中导入了服务。下面是我的数据服务导入。
import { DataService } from '../data.service';
在您的组件中创建相同的对象:
constructor(public data: DataService) { }
在我的组件中,我调用了getUser()
定义在DataService
.
this.data.getUser(email).subscribe( data => {
if(data.length > 0){
console.log(data);
}
});
以下是我的服务:
getUser(email){
// definition
}
这对我有用。
推荐阅读
- azure-cosmosdb - Cosmos 中可选字段的复合索引
- java - TextView 和 ImageView 卡出 LinearLayout
- python - 从此格式中仅提取年、月和日
- javascript - D3.js.nest 使用reduce进行累积和汇总
- react-native - React Native - 向堆栈导航添加逻辑
- apache-spark - Jupyter Notebook 上未显示结构化流输出
- c# - 正则表达式提取数字模式和冒号或换行符之间的字符串
- swift - 如何以编程方式更改 UIIMage 数组动画的颜色?
- javascript - 箭头后()括号的作用?
- reactjs - 在我更新反应状态后打印对象的值时出现错误