1

Following a tutorial, I am trying to inject a service in my component:

import { Component } from '@angular/core';
import { TodoService } from './todos.service';
import { ITodo } from './todo';
import { Http , Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';

@Component({
  selector: 'my-app',
  templateUrl: `app/app.component.html`,
  providers: [TodoService]
})
export class AppComponent  
{ 
  itodos: ITodo[];
  appTitle: string = 'To Do App';
  constructor(private _todo: TodoService);

  ngOnInit() : void {
     this._todo.gettodos()
     .subscribe(itodos => this.itodos = itodos);
  }
}

When calling TodoService, I get 'constructor implementation is missing'

My Service:

import { Injectable } from '@angular/core';
import { Http , Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/do';
import { ITodo } from './todo';

@Injectable()
export class TodoService {
   private _todoUrl='app/todos.json';
   constructor(private _http: Http){}

   gettodos(): Observable<ITodo[]> {
      return this._http.get(this._todoUrl)
      .map((response: Response) => <ITodo[]> response.json())
      .do(data => console.log(JSON.stringify(data)));
   }
}

What do I need to do to clear the error, since my TodoService is using Http?

3
  • 2
    Replace constructor(private _todo: TodoService); with constructor(private _todo: TodoService) { }. Commented Jun 21, 2018 at 0:46
  • Just to note, the Http service has been replaced with the HttpClient service (and corresponding module), so depending on your Angular version the tutorial may be out of date with what you've installed. Commented Jun 21, 2018 at 0:47
  • thanks! i knew I missed something small... Commented Jun 21, 2018 at 0:49

1 Answer 1

9

Your constructor does not have a body, just provide an empty body if you don't want to have any extra code in the constructor

constructor(private _todo: TodoService){} 
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.