首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >类型'Observable<Object>‘不可赋值给类型'any[]’。类型'Observable<Object>‘中缺少属性'includes’

类型'Observable<Object>‘不可赋值给类型'any[]’。类型'Observable<Object>‘中缺少属性'includes’
EN

Stack Overflow用户
提问于 2018-06-02 02:28:41
回答 1查看 3.6K关注 0票数 1

请帮帮我当我尝试发出http请求时出现此错误

这是我创建的服务:

代码语言:javascript
复制
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class EmployeeService {

private _url: string = "/assets/data/employee.json";

constructor(private http : HttpClient) { }

getEmployee () {
  return this.http.get(this._url);
}
}

这是我为服务订阅的组件之一:

代码语言:javascript
复制
import { Component, OnInit, Input, EventEmitter, Output  } from 
'@angular/core';
import { EmployeeService } from '../employee.service';

@Component({
  selector: '.app-test',
  template: `
    <h2>Employee List</h2>
    <ul *ngFor="let employee of employeeList">
      <li>{{employee.name}}</li>
    </ul>
  `,
 styles: [`

`]
})
export class TestComponent implements OnInit {

public employeeList = [];


@Output() public childEvent = new EventEmitter();

constructor(private _employeeService : EmployeeService) { }

fireEvent() {
  this.childEvent.emit('hey Omar');
}
ngOnInit() {
  this.employeeList = this._employeeService.getEmployee();
}
}

这是来自vs代码的错误:

不能将

类型“Observable”赋值给类型“any[]”。

'Observable‘类型中缺少属性'includes’。

enter image description here

EN

回答 1

Stack Overflow用户

发布于 2018-06-02 02:38:53

http.get返回一个可观察对象。

您需要在它的回调函数中获取数据

代码语言:javascript
复制
this._employeeService.getEmployee()
  .subscribe(data => {
      this.employeeList = data;
    });

此外,您可能希望初始化为any,以查看您得到了什么。您可能会得到一个对象,因此您可能希望从

代码语言:javascript
复制
employeeList: any;

如果它是一个数组,那么您需要修改您的http请求以反映它

代码语言:javascript
复制
this.http.get<employee_type>(this._url)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50649180

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档