好的,所以我一直在为此挣扎了一段时间。我使用名为“Contenful”的服务来充当CMS,并使用它们的API来获取JSON数据。Angular 2无法从模板访问HTTP获得响应
我有一个叫做“editor.service.ts”服务如下(我已经隐藏了“SpaceID”和“访问令牌”隐私):
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
@Injectable()
export class EditorService {
private baseURL = 'https://cdn.contentful.com';
private spaceID = 'HIDDEN';
private accessToken ='HIDDEN';
private blocksURL = this.baseURL + '/spaces/' + this.spaceID + '/entries?access_token=' + this.accessToken + '&content_type=blocks';
constructor(private http: Http) {}
getBlocks(): Observable<any> {
return this.http.get(this.blocksURL).map((res: Response) => res);
}
}
然后在我的组件调用'editor.component.ts' 我有这样的:
import { Component, OnInit } from '@angular/core';
import { EditorService } from './../editor.service';
@Component({
selector: 'app-editor',
templateUrl: './editor.component.html',
styleUrls: ['./editor.component.css'],
providers: [EditorService]
})
export class EditorComponent implements OnInit {
blocksJSON: any;
constructor(private editorService: EditorService) {}
ngOnInit() {
this.editorService.getBlocks().subscribe(res => {
this.blocksJSON = res.json().items;
console.log(this.blocksJSON); //THIS WORKS
});
}
}
这里有一个是越来越我困惑的一点。该文件中的console.log()行正好打印出我想要的内容,所以我知道该API工作正常。令人困惑的是现在。
在我的模板文件,我有这样一行:
<h1 *ngFor="let block of blocksJSON; let i = index">{{block[i].fields.blockName}}</h1>
在我看来,这应该只是工作。不过,我不断收到错误
ERROR TypeError: Cannot read property 'fields' of undefined
它打印出此错误3次,即使在目前,有在数组“块”只有1对象。但我不认为有1个对象是一个问题...(我可能是错的,虽然)
我不知道这是为什么,因为console.log打印出我想要的数据,所以我知道它应该被定义。我唯一的想法是角度2 HTTP get是异步的,但我不知道这是否是问题。
任何意见将不胜感激。谢谢。
[angular2的可能重复:错误:类型错误:无法读取undefi的属性'...' ned](https://stackoverflow.com/questions/41242793/angular2-error-typeerror-cannot-read-property-of-undefined) – Alex
和更多的建议... https://stackoverflow.com/questions/34734671/ observable-type-error-can-read-property-of-undefined大量的问答环节;) – Alex
@ AJT_82感谢您的回答并抱歉发布重复。根据2个答案,我认为我需要将h1包装在* ngIf中。我这样做如下:但我仍然得到相同的错误。你有什么想法,或者我应该继续寻找:) –