Angular和TypeScript中http.get(...).map不是函数的错误
大家好!我开始学习Angular和TypeScript,尝试向API发送一个简单的HTTP GET请求。通过CLI安装了Angular,创建了一个服务并尝试获取数据。我使用HttpClient,但在调用get方法并尝试应用RxJS的map操作符时,出现错误:"http.get(...).map is not a function in [null]"。这是我的代码:
```typescript
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class DataService {
private apiUrl = 'https://jsonplaceholder.typicode.com/posts';
constructor(private http: HttpClient) { }
getPosts() {
return this.http.get(this.apiUrl).map(
(response: Response) => response.json()
);
}
}
```
在组件中:
```typescript
import { Component, OnInit } from '@angular/core';
import { DataService } from './data.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppCo