Error "No provider for HttpClient" in Angular: causes and fix
Hi! I'm new to Angular and TypeScript. I'm trying to make an HTTP request to an API in my application, but when I run it I get the error:
```
NullInjectorError: No provider for HttpClient!
```
Here's my code. The `DataService` service:
```typescript
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class DataService {
constructor(private http: HttpClient) {}
getPosts() {
return this.http.get('https://jsonplaceholder.typicode.com/posts');
}
}
```
The `AppComponent` component:
```typescript
import { Component, OnInit } from '@angular/core';
import { DataService } from './data.service';
@Component({
selector: 'app-root',
template: '
{{ post.title }}
'
})
export No answers yet.
Log in to leave an answer
Blogs