1

I am learning to use Angular 2 and have reached a point here I am stuck with the error below. This only appears since adding the httpClientModule and trying to make a http get request from a components ngInit. The app is using angular 4.3.4.

Can't resolve all parameters for BlogComponent: (?).

contents of app.module.ts

    import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule } from '@angular/router';
import { HttpClientModule } from '@angular/common/http';


import { AppComponent } from './app.component';
import { BlogComponent } from './blog.component';
import { HomeComponent } from './home.component';


import { appRoutes } from './routes'

@NgModule({
  imports: [
    BrowserModule,
    HttpClientModule,
    RouterModule.forRoot(appRoutes)
  ],
  declarations: [
    AppComponent,
    BlogComponent,
    HomeComponent
  ],
  bootstrap: [AppComponent],

})
export class AppModule { }

contents of the blog component ts file which is throwing the error

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';


@Component({
  selector: 'test-blog',
  template: '<h1>Blog page :)</h1>',
})
export class BlogComponent implements OnInit{

  results: string[];

  constructor(private http: HttpClient) {}

  ngOnInit(): void {
    // Make the HTTP request:
    this.http.get('/api/blog/all').subscribe(data => {
      // Read the result field from the JSON response.
      this.results = data['results'];
    });
  }
}
Andrew
  • 71
  • 4

2 Answers2

0

Try with Http import rather than HttpClient :

import { Http } from '@angular/http';
...
constructor(private http: Http) {}
Boulboulouboule
  • 4,087
  • 1
  • 13
  • 29
  • The `HttpClient` class is valid refer this [**documentation***](https://angular.io/guide/http#making-a-request-for-json-data) – Aravind Nov 03 '17 at 15:12
  • 1
    It depends of the version of Angular. < 4.3 it's just Http – Boulboulouboule Nov 03 '17 at 15:20
  • I was using Http at first but I was getting other errors and then I Found the documentation above and I'm using 4.3.4 so it looks like HttpClient is the one to use. – Andrew Nov 03 '17 at 22:21
0

According to the this tutorial, your component might missing @Injectable().

Please try this:

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';


@Component({
  selector: 'test-blog',
  template: '<h1>Blog page :)</h1>',
})

@Injectable()
export class BlogComponent implements OnInit{

results: string[];

constructor(private http: HttpClient) {}

    ngOnInit(): void {
    // Make the HTTP request:
    this.http.get('/api/blog/all').subscribe(data => {
      // Read the result field from the JSON response.
      this.results = data['results'];
        });
    }
}
DeckerCHAN
  • 57
  • 1
  • 5
  • You already [get this for free](https://stackoverflow.com/questions/36494960/component-as-injectable) by using the `Component` decorator (`@Component`). – Kirk Larkin Nov 26 '17 at 16:23