我的服务遇到问题。我有一个服务,它从 HTTP 模块获取 JSON 并用它填充类“olders”。但是当我打电话给我的服务时,它没有做任何事情,我的班级长辈仍然未定义......
如果我在yearbook.component.html中创建一个*ngFor指令,我什么也得不到...... 我认为我的问题来自我的服务,但我不知道错误在哪里......
即使我将 console.log 放在订阅中
我的年鉴.组件:
import {Component} from '@angular/core'
import {Olders} from './olders'
import {OldersService} from './olders.service'
@Component({
moduleId: module.id,
selector: 'yearbook',
templateUrl: 'yearbook.component.html',
})
export class YearbookComponent {
olders : Olders[];
constructor(
private _oldersService : OldersService
){}
ngOnInit() {
this._oldersService.getOldersfromAPI()
.subscribe( res => {this.olders = res,
console.log(this.olders)},
err => console.error(err.status)
);
}
}
和老年人服务:
import {Injectable} from '@angular/core'
import {Http} from '@angular/http'
import {Olders} from './olders'
import 'rxjs/add/operator/map'
import 'rxjs/add/operator/do'
@Injectable ()
export class OldersService {
constructor(private _http:Http) {
}
getOldersfromAPI(){
return this._http.get('../CDN/olders.json')
.do(x => console.log(x))
.map(olders => {olders = olders.json()});
}
}
预先感谢您的所有回答
您的映射中缺少 return 语句:
.map(olders => {olders = olders.json()});
应该是:
.map(olders => { return olders = olders.json()});