Search code examples
angularangular7

How to change the http module from angular 4 to angular 7


I was following on of angular 4 tutorials. But I need to modify it to angular 7. But following code segment gives an error on Http module and Observable

This is the user.service.ts File

import { Injectable } from '@angular/core';
import{Http, Response, Headers, RequestOptions} from '@angular/http';
import{Observable}   from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import{User}  from '../user';


@Injectable()
export class UserService {
  private baseUrl:string='http://localhost:8080/api';
  private headers = new Headers({'Content-Type':'application/json'});
  private options = new RequestOptions({headers:this.headers});
  private user = new User();
  constructor(private _http:Http) { }

  getUsers(){

    return this._http.get(this.baseUrl+'/users',this.options).map((response:Response)=>response.json())
      .catch(this.errorHandler);
  }
  getUser(id:Number){

    return this._http.get(this.baseUrl+'/user/'+id,this.options).map((response:Response)=>response.json())
      .catch(this.errorHandler);
  }
  deleteUser(id:Number){

    return this._http.delete(this.baseUrl+'/user/'+id,this.options).map((response:Response)=>response.json())
      .catch(this.errorHandler);
  }


  createUser(user:User){

    return this._http.post(this.baseUrl+'/user',JSON.stringify(user),  this.options).map((response:Response)=>response.json())
      .catch(this.errorHandler);
  }

   updateUser(user:User){

    return this._http.put(this.baseUrl+'/user',JSON.stringify(user),  this.options).map((response:Response)=>response.json())
      .catch(this.errorHandler);
  }

  errorHandler(error:Response){

     return Observable.throw(error||"SERVER ERROR");
  }

   setter(user:User){
     this.user=user;
   }

  getter(){
    return this.user;
  }
}

What should be changed in order to run this on angular 7? The above code segment is from angular 4. An answer is really appreciated


Solution

  • From my experience, Maven is what is the most widely used. I actually just finished up a large Spring project in conjunction with Maven.

    I work at a company that employs over 100,000 individuals, and the only support provided is for Maven (at an enterprise level). This is anecdotal, yes, but I can say that my university prioritized Maven, and all my past and present colleagues have used Maven.

    This is not to say, however, that Gradle or Ant are not solid choices. There are many articles detailing the differences. See: https://www.baeldung.com/ant-maven-gradle