Angular 5 Type脚本。
我遇到一个问题,即使用地图功能时,无法访问Dog []的属性。我的目标是获取json响应,解析ID并使用该响应创建多个新请求。我已经编写了两个调用两个不同终结点的http服务。
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Dog } from '../dog'
import { DogService } from '../dog.service'
import { DogpagesService } from '../dogpages.service'
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/mergeMap';
import 'rxjs/add/operator/switch';
@Component({
selector: 'app-girls',
templateUrl: './girls.component.html',
styleUrls: ['./girls.component.css']
})
export class GirlsComponent implements OnInit {
dogs: Observable<Dog[]>;
constructor(
private dogService: DogService,
private dogpagesService: DogpagesService,
) {
}
ngOnInit() {
this.dogpagesService.getPage('girls')
.map(res =>
res.map((val) => {
console.log(val)
return val.DogsId
}))
.subscribe(obsNumbers => obsNumbers
.map(num => this.dogService
.getDog(num)
.subscribe(dog => console.log(dog)
)));
}
}
以下内容将记录到控制台。
{dogsId: 159, pageName: "Girls", sortId: 2}
{dogsId: 11, pageName: "Girls", sortId: 1}
GET https://localhost:44342/api/dogs/undefined 400
HttpErrorResponse {headers: HttpHeaders, status: 400, statusText: "OK", url: "https://localhost:44342/api/dogs/undefined", ok: false, …}
为什么在console.log(val)期间可以使用dogsID,但是以后再订阅时却不可用?
谢谢。