首页 > 解决方案 > 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);
  }

标签: apicallangular6

解决方案


检查您是否在组件中导入了服务。下面是我的数据服务导入。

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
}

这对我有用。


推荐阅读