【发布时间】:2017-10-23 21:57:50
【问题描述】:
这里是源代码:
export class PostComponent implements OnInit {
posts: any[];
constructor(private http: Http, private service: PostService) {
}
getAll1() {
const url = 'http://jsonplaceholder.typicode.com/posts';
this.http.get(url)
.map((respone) => respone.json())
.subscribe(posts => this.posts = posts );
}
}
`
它工作正常,但是如果我为“respone.json”添加花括号,那么视觉标准输出代码会提示错误(见下文)。 添加大括号后的源代码如下:
export class PostComponent implements OnInit {
posts: any[];
constructor(private http: Http, private service: PostService) {
}
getAll1() {
const url = 'http://jsonplaceholder.typicode.com/posts';
this.http.get(url)
.map((respone) => {respone.json();})
.subscribe(posts => this.posts = posts );
}
}
错误出现在最后一行的单词“this.posts”上,错误是:[ts] Type 'void' is notassignable to type 'any[]'。
为什么?
【问题讨论】:
-
{response.json();}不返回值 -
您似乎在使用 Typescript,将其添加为标签可能会获得更多点击。
标签: javascript angular rxjs