error in http post

1

I would like to have the save method in the class itself, and I'm not getting it, I'm a beginner in the typeScript language.

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


    @Injectable()

    export class Usuario {

      http: Http;
      nome: string;
      apelido: string;
      email: string;
      tel: string;
      senha: string;

      salvar(usuario){
        this.http.post("http://localhost:5000/users",JSON.stringify(usuario), {})
                 .toPromise()
                 .then(res => console.log(res.json().data))
                 .catch();
      }

    }

I get the error:

    browser_adapter.js:84 ORIGINAL EXCEPTION: TypeError: Cannot read property 'post' of undefined
    
asked by anonymous 10.08.2016 / 17:04

1 answer

0

You forgot to inject Http .

constructor(private http: Http) {
  }

And then on your AppModule

import { HttpModule } from '@angular/http';

...

 imports:[
  HttpModule,
],
...
    
10.01.2017 / 17:04