2

I'm just playing around with services in Angular for the first time and I get the following error:

ERROR TypeError: this.httpClientModule.get is not a function

my services file:

import { Injectable } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';

@Injectable()
export class AlertsService {
    constructor(
        private httpClientModule: HttpClientModule
    ) {
        //
    }

    getAlerts() {
        console.warn(this.httpClientModule.get('api-data/alerts-data.json'));
    }
}

component file:

import { Component, OnInit, } from '@angular/core';
import { AlertsService } from '../alerts.service'

@Component({
    selector: 'at-alerts',
    templateUrl: './alerts.component.html',
    styleUrls: ['./alerts.component.scss'],
    providers: [AlertsService]
})

export class AlertsComponentEdit implements OnInit {
    constructor(
        private alertsService: AlertsService
    ) {
        //
    }

    ngOnInit() {
        this.alertsService.getAlerts();
    }
}

Mock data file (alerts-data.json):

[{
  "id": 1,
  "name": "Louis",
  "gender": "male"
}, {
  "id": 2,
  "name": "Jenna",
  "gender": "female"
}, {
  "id": 3,
  "name": "Tom",
  "gender": "male"
}];

From what I can tell get along with other methods like post etc should exist in httpClientModule, any idea how to resolve this error?

1
  • "playing around for the first time", maybe taking a look at the docs would be reasonable? angular.io/guide/http Commented Dec 18, 2017 at 17:53

1 Answer 1

3

There is nothing named get method inside httpClientModule.You need to inject HttpClient inside your constructor

constructor(private http: HttpClient) {}     

getAlerts() {
    console.warn(this.http.get('api-data/alerts-data.json'));
}

Also import the HttpClientModule in your app.module.ts

import { HttpClientModule } from '@angular/common/http';

so your app.module will look like,

import { NgModule } from '@angular/core';
import {HttpClientModule} from '@angular/common/http';

@NgModule({
  imports: [
    HttpClientModule
  ],
  declarations: [
  ],
  exports: [
  ]
})
export class CoreModule { }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.