Javascript 从GithubAPI获取数据
我想从GithubAPI获取所有数据。但这对我不起作用。 我的.ts文件如下:Javascript 从GithubAPI获取数据,javascript,angular,Javascript,Angular,我想从GithubAPI获取所有数据。但这对我不起作用。 我的.ts文件如下: import { Component } from '@angular/core'; import { GitTakeService } from "app/git-take.service"; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'
import { Component } from '@angular/core';
import { GitTakeService } from "app/git-take.service";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
user:any;
constructor(private gittakeService:GitTakeService ){
this.gittakeService.getUser().subscribe(user=>{
debugger;
this.user=user;
console.log(user);
})
}
}
import { Injectable } from '@angular/core';
import {Http,Response, Headers} from '@angular/http'
import'rxjs/add/operator/map';
@Injectable()
export class GitTakeService {
constructor(private http:Http) { }
getUser(){
debugger;
return this.http.get("http://api.github.com/users")
.map(
(resp:Response)=>{
return resp.json().response;
}
);
}
}
我的服务如下:
import { Component } from '@angular/core';
import { GitTakeService } from "app/git-take.service";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
user:any;
constructor(private gittakeService:GitTakeService ){
this.gittakeService.getUser().subscribe(user=>{
debugger;
this.user=user;
console.log(user);
})
}
}
import { Injectable } from '@angular/core';
import {Http,Response, Headers} from '@angular/http'
import'rxjs/add/operator/map';
@Injectable()
export class GitTakeService {
constructor(private http:Http) { }
getUser(){
debugger;
return this.http.get("http://api.github.com/users")
.map(
(resp:Response)=>{
return resp.json().response;
}
);
}
}
在.ts文件中对用户进行控制台操作时,它显示未定义。我的视图文件如下所示:
getUser(){
debugger;
return this.http.get("http://api.github.com/users")
.map((resp:Response)=>{
return resp.json();
});
}
{{user}}
有人能帮我解决这个问题吗?resp.json()。响应未定义resp.json()
是您想要的
服务功能:
getUser(){
return this.http.get("http://api.github.com/users")
.map(
(resp:Response)=>{
return resp.json();
}
);
}`
和组件:
this.gittakeService.getUser().subscribe(users=>{
this.user=users[0];
console.log(user);
})
您正在接收的是一个数组,因此您希望使用resp.json()
而不是resp.json().response
在响应中没有类似response
的属性。因此,您的地图应如下所示:
getUser(){
debugger;
return this.http.get("http://api.github.com/users")
.map((resp:Response)=>{
return resp.json();
});
}
在您的组件中,我将把数组命名为users
,而不是user
,因为您的响应中有多个用户。此外,我建议您保留构造函数中任何不必要的内容,并改用OnInit
:
users = [];
constructor(private gittakeService:GitTakeService ){ }
ngOnInit() {
this.gittakeService.getUser()
.subscribe(data => {
this.users = data;
});
}
然后,您可以迭代该数组,并使用属性名称来显示一个用户
对象的属性:
<div *ngFor="let user of users">
{{user.login}}
</div>
{{user.login}
您可以控制台日志(resp)代码>?响应是安慰性的。响应是github数据。但它没有订阅?sir它的安慰性很好。sir如何将所有数据显示到viewisresp.json()。响应
consoling fine?【对象对象对象】、【对象对象对象】、【对象对象对象对象】、【对象对象对象对象对象对象】、【对象对象对象对象对象对象】、【对象对象对象】、【对象对象】,[对象对象],[对象对象对象],[对象对象],[对象对象对象],[对象对象对象],[对象对象对象],[对象对象对象],[对象对象对象],[对象对象对象],[对象对象对象],[对象对象对象对象],[对象对象对象对象对象],[对象对象对象],[对象对象对象对象对象对象对象]像这样,解决方案是什么{{user}}这是我的观点