[英]Mapping the response to a variable angular 5
我正在使用HttpClient調用get請求,該請求以以下格式返回響應。
{
"organizations": [
{
"id": 1,
"name": "Team Red",
"address": null,
"sites": [
{
"organization": 1,
"name": "Site A",
"address": null,
"lat": null,
"lon": null,
"id": 1,
"createdAt": "2017-10-23T11:07:11.000Z",
"updatedAt": "2017-10-23T11:07:11.000Z"
},
{
"organization": 1,
"name": "Site B",
"address": null,
"lat": null,
"lon": null,
"id": 2,
"createdAt": "2017-10-23T11:07:11.000Z",
"updatedAt": "2017-10-23T11:07:11.000Z"
},
{
"organization": 1,
"name": "Site C",
"address": null,
"lat": null,
"lon": null,
"id": 3,
"createdAt": "2017-10-23T11:07:11.000Z",
"updatedAt": "2017-10-23T11:07:11.000Z"
}
]
},
{
"id": 2,
"name": "Team Blue",
"address": null,
"sites": [
{
"organization": 2,
"name": "Site X",
"address": null,
"lat": null,
"lon": null,
"id": 4,
"createdAt": "2017-10-23T11:07:11.000Z",
"updatedAt": "2017-10-23T11:07:11.000Z"
},
{
"organization": 2,
"name": "Site Y",
"address": null,
"lat": null,
"lon": null,
"id": 5,
"createdAt": "2017-10-23T11:07:11.000Z",
"updatedAt": "2017-10-23T11:07:11.000Z"
},
{
"organization": 2,
"name": "Site Z",
"address": null,
"lat": null,
"lon": null,
"id": 6,
"createdAt": "2017-10-23T11:07:11.000Z",
"updatedAt": "2017-10-23T11:07:11.000Z"
}
]
}
]
}
我需要直接將響應映射到組織變量。 我已經附上了服務文件代碼,以及組織和站點文件。 我收到的錯誤是在response.json()上,並且我搜索到此功能不可用。 任何幫助將不勝感激。
import {Site} from "./Site";
export class Organization {
id: number;
name: string;
address: string;
sites: Site[];
constructor(values: Object = {}) {
Object.assign(this, values);
}
}
---------
export class Site {
organization: number;
name: string;
address: string;
lat: string;
lon: number;
id: number;
createdAt: string;
updatedAt: number;
}
---- API Service ----
import {Injectable} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {Organization} from "../models/Organization";
import 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
const API_URL = "http://localhost:1337/api";
@Injectable()
export class ApiService {
constructor(private http: HttpClient) {
}
public getOrganizationWithSites(): Observable<Organization[]> {
const headers = new HttpHeaders()
.set("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MTEsIm5hbWUiOiJBdGlmIiwiaWF0IjoxNTIwMjY2NDQwLCJleHAiOjE1MjAzNTI4NDB9.GW5P7zDow4PcI9RaB3pn6KxPn929pmzPCnce6n8OCo8");
let organizations: any;
var options = {
headers: headers
};
return this.http
.get<Organization[]>(API_URL + '/user/organizations', options);
}
// private handleError(error: Response | any) {
// console.error('ApiService::handleError', error);
// return Observable.throw(error);
// }
}
// Components Code
ngOnInit() {
this.apiService
.getOrganizationWithSites()
.subscribe((data) => {
this.organizations = data;
});
console.log(this.organizations);
}
import { HttpClientModule } from '@angular/common/http';
在4.3.0版中引入了HttpClient API。 它是現有HTTP API的發展,並具有自己的軟件包@ angular / common / http。 最值得注意的變化之一是現在響應對象默認為JSON,因此不再需要使用map方法進行解析。直接我們可以像下面這樣使用
return http.get(API_URL + '/user/organizations', options);
然后在您的組件中
@Component({
selector: 'my-component',
templateUrl: './MyComponent.component.html'
})
export class MyComponent{
public organizations : Organization[] = [];
constructor(private apiService: ApiService) {
this.apiService
.getOrganizationWithSites()
.subscribe((data) => {
this.organizations = data;
console.log(this.organizations);
});
}
}
從response.json();
刪除json
部分response.json();
在HttpClient angular 5中的ApiService中不需要這樣做。 HttpClient
隱式地執行此操作。
有關更多信息Angular 5
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.