【发布时间】:2019-05-21 17:39:34
【问题描述】:
尝试从 Web Api 获取数据数组。当我第一次调用 api 时,结果返回指示未定义。之后的任何调用都会按预期获得结果。
Oncall 方法将结果存储到工作站
@Injectable()
export class StationService {
private stations;
constructor(private webservice: WebService) { }
Oncall() {
this.webservice.GetData('Search/GetStations').subscribe((respons: Station[]) => { this.stations = respons; });
console.log(this.stations);
console.log('----------------------------------');
}
}
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Station } from '../dataModels/stationModel';
import { map } from 'rxjs/operators';
@Injectable()
export class WebService {
private url = 'http://localhost:11835/Api/';
constructor(private http: HttpClient) { }
GetData(path: string) {
return this.http.get<Station[]>((this.url + path)).pipe(
map((data: Station[]) => data)
);
}
}
谢谢
【问题讨论】: