请帮帮我当我尝试发出http请求时出现此错误
这是我创建的服务:
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);
}
}
这是我为服务订阅的组件之一:
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’。
发布于 2018-06-02 02:38:53
http.get返回一个可观察对象。
您需要在它的回调函数中获取数据
this._employeeService.getEmployee()
.subscribe(data => {
this.employeeList = data;
});
此外,您可能希望初始化为any,以查看您得到了什么。您可能会得到一个对象,因此您可能希望从
employeeList: any;
如果它是一个数组,那么您需要修改您的http请求以反映它
即
this.http.get<employee_type>(this._url)
https://stackoverflow.com/questions/50649180
复制相似问题