【问题标题】:ReferenceError: data is not defined in angular 6ReferenceError:数据未在角度 6 中定义
【发布时间】:2018-07-18 12:34:53
【问题描述】:

我正在通过 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);
  }

【问题讨论】:

    标签: api call angular6


    【解决方案1】:

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

    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
    }
    

    这对我有用。

    【讨论】:

    • 感谢您的贡献
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-30
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多