[Fixed] Angular Service and HttpClient Type does not exist

Issue

I am trying to get a service working in Angular 5. This is what I have:

import { Injectable } from '@angular/core';


@Injectable()
export class DataService {

  constructor() { }

  getData() {
    // don't use 'any', type your data instead!
    return this.httpClient.get<any>('./assets/data.json');
  }

}

I’m getting the following error:

Property HttpClient does not exist on type DataService.

What am I missing?

Solution

Try like this :

readmore about httpClient here

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

@Injectable()
export class DataService {

  constructor(private httpClient: HttpClient) { }

  getData() {
    // don't use 'any', type your data instead!
    return this.httpClient.get<any>('./assets/data.json');
  }

}

Leave a Reply

(*) Required, Your email will not be published