Keep getting a "Cannot find name 'subscribe'" error when trying to subscribe to data from http link (Angular)

huangapple go评论66阅读模式
英文:

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 &#39;@angular/core&#39;;
import { HttpClient } from &#39;@angular/common/http&#39;;

@Component({
  selector: &#39;app-root&#39;,
  template: &#39;
    &lt;!-- header --&gt;
    &lt;app-header&gt;&lt;/app-header&gt;
   
    &lt;!-- routes get injected here--&gt;
    &lt;router-outlet&gt;&lt;/router-outlet&gt;

    &lt;!-- footer --&gt;
    &lt;app-footer&gt;&lt;/app-footer&gt;


 &#39;,
  styles: []
})
export class AppComponent {
  
  constructor(private http: HttpClient){}

  ngOnInit() {
    this.http.get(&#39;https://api.github.com/users?per_page=10&#39;);
      .subscribe(data =&gt; { //this is the line that is giving me the errors.
        console.log(data);
      });
  }
}

答案1

得分: 1

这一行上有一个分号放置不正确:

this.http.get(&#39;https://api.github.com/users?per_page=10&#39;);
英文:

You have a semi-colon incorrectly placed on this line:

this.http.get(&#39;https://api.github.com/users?per_page=10&#39;);

huangapple
  • 本文由 发表于 2023年8月4日 04:08:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831324.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定