英文:
Keep getting a "Cannot find name 'subscribe'" error when trying to subscribe to data from http link (Angular)
问题
我一直在尝试学习Angular,并观看了一个教程系列。本集试图教你如何从GitHub用户那里获取数据,以在网页上创建一个用户列表(https://www.youtube.com/watch?v=w755PKU5DZg&list=PLseEp7p6EwiajxL9R4_o2dF7q3LxLWPFb&index=13)。
我已经到处查找,但找不到我与教程不同的地方。有人知道为什么会出现这些错误吗?("找不到名称'subscribe'","需要声明或语句",以及"参数'data'隐式具有'any'类型"
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-root',
template: `
<!-- header -->
<app-header></app-header>
<!-- routes get injected here-->
<router-outlet></router-outlet>
<!-- footer -->
<app-footer></app-footer>
`,
styles: []
})
export class AppComponent {
constructor(private http: HttpClient){}
ngOnInit() {
this.http.get('https://api.github.com/users?per_page=10')
.subscribe(data => { // 这是给我报错的那一行。
console.log(data);
});
}
}
英文:
I have been trying to learn angular and have been watching a tutorial series. This episode is trying to teach how to get data from GitHub users, to create a user list on the webpage (https://www.youtube.com/watch?v=w755PKU5DZg&list=PLseEp7p6EwiajxL9R4_o2dF7q3LxLWPFb&index=13).
I have looked everywhere but can't find a single thing I have done differently from the tutorial. Does anyone know why I am getting these errors? ("Cannot find name 'subscribe'" "Declaration or statement expected" and "parameter data implicitly has an 'any' type"
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-root',
template: '
<!-- header -->
<app-header></app-header>
<!-- routes get injected here-->
<router-outlet></router-outlet>
<!-- footer -->
<app-footer></app-footer>
',
styles: []
})
export class AppComponent {
constructor(private http: HttpClient){}
ngOnInit() {
this.http.get('https://api.github.com/users?per_page=10');
.subscribe(data => { //this is the line that is giving me the errors.
console.log(data);
});
}
}
答案1
得分: 1
这一行上有一个分号放置不正确:
this.http.get('https://api.github.com/users?per_page=10');
英文:
You have a semi-colon incorrectly placed on this line:
this.http.get('https://api.github.com/users?per_page=10');
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论