Can you help me with this issue? Here is the code for the user service:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { User } from './user';
@Injectable()
export class UserService {
private _url = "https://jsonplaceholder.typicode.com/users";
constructor(private _httpClient: HttpClient) {
}
getUser(id: string){
return this._httpClient.get<User>(this._url + '/' + id);
}
}
The getUser method should return an Observable of a User object. However, when I try to get the User object in my component, it retrieves the entire user object instead of the one defined in my user.ts file. Below is the code for user.ts:
export class Address {
street: string;
suite: string;
city: string;
zipcode: string;
}
export class User {
id: number;
name: string;
phone: string;
email: string;
address = new Address();
}
When I call the service from the component, I am unable to retrieve the User object as expected. Here is the component code:
import { Component, OnInit } from '@angular/core';
import { UserService } from './user.service';
import { Router, ActivatedRoute } from '@angular/router';
@Component({
selector: 'newuserform',
templateUrl: './newuser-form.component.html',
providers: [UserService]
})
export class NewUserFormComponent implements OnInit {
constructor(
private _service: UserService,
private _router: Router,
private _route: ActivatedRoute) { }
ngOnInit(){
let id = this._route.snapshot.paramMap.get('id');
this._service.getUser(id)
.subscribe(
user => console.log(user);
);
}
}