call get on ionic 2

0

Good, I am new in this of angular 2 and ionic 2 and I do not understand well the theory of the constructors and some things.

I have the following code:

import { Injectable } from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {Http} from '@angular/http';
import 'rxjs/add/operator/map';
  


 
export class User {
  name: string;
  email: string;
 
  constructor(name: string, email: string) {
    this.name = name;
    this.email = email;
  }
}
 
@Injectable()
export class AuthService {
  currentUser: User;

  public login(credentials) {
    if (credentials.email === null || credentials.password === null) {
      return Observable.throw("Please insert credentials");
    } else {
      
 var url = 'https://apibioidcrear.azurewebsites.net/Usuario/GetUsuario/2';
 var response = this.http.get(url).map(res => res.json());
 console.log(response);

      return Observable.create(observer => {
        // At this point make a request to your backend to make a real check!
        let access = (credentials.password === "pass" && credentials.email === "email");
        this.currentUser = new User('Alexis Nichel', '[email protected]');
        observer.next(access);
        observer.complete();
      });
    }
  }
 
  public register(credentials) {
    if (credentials.email === null || credentials.password === null) {
      return Observable.throw("Please insert credentials");
    } else {
      // At this point store the credentials to your backend!
      return Observable.create(observer => {
        observer.next(true);
        observer.complete();
      });
    }
  }
 
  public getUserInfo() : User {
    return this.currentUser;
  }
 
  public logout() {
    return Observable.create(observer => {
      this.currentUser = null;
      observer.next(true);
      observer.complete();
    });
  }
}

and I can not get this part to work:

 var url = 'https://apibioidcrear.azurewebsites.net/Usuario/GetUsuario/2';
 var response = this.http.get(url).map(res => res.json());
 console.log(response);

I can not make myself recognize the http

    
asked by Alexis Granja 13.01.2017 в 17:02
source

1 answer

2

In the constructor of your class where you define constructor(name: string, email: string) , you should create a variable http that refers to the data type Http that you are importing from the Angular :

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

It would be as follows:

constructor (name: string, email: string, http: Http)
    
answered by 14.01.2017 / 19:13
source