通过导入文件,我得到“类型'observable不是通用的'”
我正在尝试用角度7进行CRUD操作,并制作了两个组成部分:雇员的添加和列表,而我创建和获取雇员列表的功能写在“ policy.service.ts”中,在这里我得到了“ type”可观察到的“不是通用的”,我没有走错地方,请帮助解决此问题
这是我的pacakage.json
{
"name": "crudop",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "~7.2.0",
"@angular/common": "~7.2.0",
"@angular/compiler": "~7.2.0",
"@angular/core": "~7.2.0",
"@angular/forms": "~7.2.0",
"@angular/platform-browser": "~7.2.0",
"@angular/platform-browser-dynamic": "~7.2.0",
"@angular/router": "~7.2.0",
"angular-in-memory-web-api": "^0.8.0",
"bootstrap": "^4.3.1",
"core-js": "^2.5.4",
"rxjs": "~6.3.3",
"rxjs-compat": "^6.5.2",
"tslib": "^1.9.0",
"zone.js": "~0.8.26"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.13.0",
"@angular/cli": "~7.3.9",
"@angular/compiler-cli": "~7.2.0",
"@angular/language-service": "~7.2.0",
"@types/node": "~8.9.4",
"@types/jasmine": "~2.8.8",
"@types/jasminewd2": "~2.0.3",
"codelyzer": "~4.5.0",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~4.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~1.1.2",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.4.0",
"ts-node": "~7.0.0",
"tslint": "~5.11.0",
"typescript": "~3.2.2"
}
}
policy.service.ts
import {
Injectable
} from '@angular/core';
import {
HttpClient
} from '@angular/common/http';
import {
HttpHeaders
} from '@angular/common/http';
import {
IEmployee
} from './Iempolyee';
import {
Observable
} from 'rxjs/Observable';
@Injectable({
providedIn: 'root'
})
export class PolicyService {
constructor(private http: HttpClient) {}
private url = 'api/employees';
getEmployees(): Observable < IEmployee[] > {
return this.http.get < IEmployee[] > (this.url)
}
createUseremployee(user: IEmployee): Observable < IEmployee > {
const httpOptions = {
headers: new HttpHeaders({
'Content-
Type ':'
application / json '})}
return this.http.post < IEmployee > (this.url, user, httpOptions);
}
}
在这里,我遇到类型为observable的getEmployee和createUseremployee函数的问题。
我在考虑是否需要重新安装全部或任何技巧,谢谢
答案 0 :(得分:0)
您需要为{observable}添加{},并且仅从rxjs导入
import { Observable } from 'rxjs';
这对我有用
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { HttpHeaders} from '@angular/common/http';
import { Observable } from 'rxjs';
export interface IEmployee {
id:number
}
@Injectable({
providedIn: 'root'
})
export class PolicyService {
constructor(private http: HttpClient) { }
private url = 'api/employees';
getEmployees(): Observable<IEmployee[]> {
return this.http.get<IEmployee[]>(this.url)
}
createUseremployee(user: IEmployee): Observable<IEmployee> {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type ': 'application / json '
})
}
return this.http.post<IEmployee>(this.url, user, httpOptions);
}
答案 1 :(得分:0)
感谢帮助人员,但最后我从头开始像创建文件夹一样重新启动所有东西并安装了所有内容,这很奇怪,但是之后事情开始起作用并且没有出现相同的问题。
再次感谢