【发布时间】:2018-07-18 12:49:21
【问题描述】:
我是 Angular 框架和 TypeScript 的新手。我正在尝试在this tutorial 之后发出 GET 请求。它对我有用,但是当我尝试发出另一个 GET 请求时,如下所示,我收到下一个错误:
AppComponent.html:7 ERROR TypeError: Cannot read property 'Classes' of undefined
at Object.eval [as updateRenderer] (AppComponent.html:7)
at Object.debugUpdateRenderer [as updateRenderer] (core.js:14735)
at checkAndUpdateView (core.js:13849)
at callViewAction (core.js:14195)
at execComponentViewsAction (core.js:14127)
at checkAndUpdateView (core.js:13850)
at callWithDebugContext (core.js:15098)
at Object.debugCheckAndUpdateView [as checkAndUpdateView] (core.js:14635)
at ViewRef_.detectChanges (core.js:11619)
at eval (core.js:5918)
我有这个 app.component.ts:
import { Component, onInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
posts: any;
Classes: string;
readonly ROOT_URL = 'http://localhost:8080/[path]';
constructor(private http: HttpClient){
}
getPosts() {
this.posts = this.http.get(ROOT_URL + "/getClasses");
}
}
在 app.component.html 中:
<button (click)="getPosts()">Get Posts</button>
<div>
{{ posts.Classes | json }}
</div>
我尝试获取的 JSON 是:
{"Classes":["Element1","Element2","Element3"]}
在我添加了调用 getPosts() 的行之后,我仍然得到同样的错误。
【问题讨论】:
-
你在哪里调用 getPosts()?
标签: html css angular typescript httpclient