Angular 角度2:属性';地图';不存在于类型';可观察<;回应>';
我在发出get请求时收到以下错误 模块“'node_modules/rxjs/Observable'”没有导出的成员“Observable”。 类型“Observable”上不存在属性“map” 我已经尝试了StackOverflow中提供的所有可能的解决方案,但它对我不起作用 代码:Angular 角度2:属性';地图';不存在于类型';可观察<;回应>';,angular,Angular,我在发出get请求时收到以下错误 模块“'node_modules/rxjs/Observable'”没有导出的成员“Observable”。 类型“Observable”上不存在属性“map” 我已经尝试了StackOverflow中提供的所有可能的解决方案,但它对我不起作用 代码: import { Injectable } from '@angular/core'; import { IEmployee } from './employee'; import { Http
import { Injectable } from '@angular/core';
import { IEmployee } from './employee';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
@Injectable()
export class EmployeeService {
private _url : string = 'apiData/employeedata.json'
constructor(private _http: Http) { }
getEmployees(): Observable<IEmployee[]> {
return this._http.get(this._url)
.map((response: Response) => <IEmployee[]>response.json());
}
}
从'@angular/core'导入{Injectable};
从“/employee”导入{ieemployee};
从'@angular/Http'导入{Http,Response};
从“rxjs/Observable”导入{Observable};
导入'rxjs/add/operator/map';
@可注射()
出口级雇员服务{
private\u url:string='apiData/employeedata.json'
构造函数(私有http:http){}
getEmployees():可观察{
返回此。_http.get(此。_url)
.map((response:response)=>response.json());
}
}
您使用的是角度6而不是角度2您正在使用的
HttpModule
已被弃用您应该使用而不是在新的
HttpClientModule
JSON是假定的默认值,不再需要使用res.JSON()
在使用
HttpClient
之前,需要将角度HttpClientModule
导入根模块
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';
@NgModule({
imports: [
BrowserModule,
HttpClientModule,
],
//.......
您可以告诉HttpClient
响应的类型,以便更轻松、更明显地使用输出
可以使用Type参数对响应进行类型检查
Http
返回一个observable
,当我们使用Http.get(…)
时,我们可以告诉HttpClient.get
返回response
作为IEmployee类型,然后它返回observable
类型的实例在组件
中,订阅到可观察的以获取IEmployee的实例
为您服务
import { Injectable } from '@angular/core';
import { IEmployee } from './employee';
import { HttpClient} from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class EmployeeService {
private _url : string = 'apiData/employeedata.json'
constructor(private _http: HttpClient) { }
getEmployees(): Observable<IEmployee[]> {
return this._http.get<IEmployee[]>(this._url);
}
}
并将.map(…)更改为.pipe(map(…))
PS:如果您使用的是HttpClientModule
修改代码
return this._http.get(this._url).pipe(
map((response: Response) => <IEmployee[]>response.json()));
返回此.\u http.get(此.\u url).pipe(
map((response:response)=>response.json());
我建议您停止使用HttpModule
,转而使用HttpClientModule
您的rxjs
版本是什么?您使用的是哪个Angular版本?Angular CLI:6.0.5节点:8.11.2 OS:win32 x64 Angular:6.0.3。。。动画,通用,编译器,编译器cli,核心,窗体。。。http,语言服务,平台浏览器。。。平台浏览器动态,routerPackage版本--------------------------------------------@angular devkit/architect 0.6.5@angular devkit/build angular 0.6.5@angular devkit/build optimizer 0.6.5@angular devkit/core 0.6.5@angular devkit/schematics 0.6.5@angular/cli 6.0.5@ NGoToo/WebPACK.0.0.5@示意图/角度0.65.@示意图/更新0.65.RXJS2.2.0 TyrScript 2.7.2 WebPACK 4.83HI如果这个答案解决了您的问题请考虑通过点击复选标记来接受它。这向更广泛的社区表明,你已经找到了一个解决方案,并给回答者和你自己带来了一些声誉。没有义务这样做。
return this._http.get(this._url).pipe(
map((response: Response) => <IEmployee[]>response.json()));