angular - 找不到不同的支持对象'响应状态:200
问题描述
我想从 github 恢复数据并显示这些数据,但它给了我错误。this.followers
文件中的编辑器代码下划线github-followers.component.ts
。
github-followers.component.ts
import { GithubFollowersService } from './../services/github-followers.service';
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'github-followers',
templateUrl: './github-followers.component.html',
styleUrls: ['./github-followers.component.css']
})
export class GithubFollowersComponent implements OnInit {
followers : any[];
constructor(private service:GithubFollowersService) { }
ngOnInit() {
this.service.getAll()
.subscribe(followers => this.followers = followers);
}
}
github-followers.service.ts
import { Http } from '@angular/http';
import { DataService } from './data.service';
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class GithubFollowersService extends DataService {
constructor(http:Http) {
super('https://api.github.com/users/IDBRAHIMDEV/followers',http)
}
}
数据服务.ts
import { BadInput } from './../common/bad-input';
import { AppError } from './../common/app-error';
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Observable } from 'rxjs';
import { NotFoundError } from '../common/not-found-error';
import { catchError } from 'rxjs/operators';
import { throwError } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class DataService {
constructor(private url : string ,private http : Http) { }
getAll(){
return this.http.get(this.url).
pipe(
catchError(
this.handleError
)
)}
create(resource){
return this.http.post(this.url,resource).
pipe(
catchError(
this.handleError
)
)
}
update(resource){
return this.http.put(this.url,resource).
pipe(
catchError(
this.handleError
)
)
}
delete(resource){
return this.http.delete(this.url+'/'+resource.id).
pipe(
catchError(
this.handleError
)
)
}
private handleError(error : Response){
if(error.status===404){
return throwError(new NotFoundError(error));
}
if(error.status===400){
return throwError(new BadInput(error))
}
return throwError(new AppError(error))
}
}
github-followers.component.html
<div *ngFor="let follower of followers" class="media">
<div class="media-left">
<a href="#">
<img class="avatar media-object" src="{{ follower.avatar_url }}">
</a>
</div>
<div class="media-body">
<h4 class="media-heading">{{ follower.login }}</h4>
<a href="follower.html_url">{{ follower.html_url }}</a>
</div>
</div>
解决方案
如果您使用 Angular 8 而不是:
import { Http } from '@angular/http';
你应该使用:
import { HttpClient, HttpHeaders } from '@angular/common/http';
但我认为您在响应有效负载中收到的对象不是数组。
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
import { HelloComponent } from './hello.component';
import { GithubFollowersComponent } from './components/github-
followers/github-followers.component';
import { DataService } from './services/data.service';
@NgModule({
imports: [ BrowserModule, FormsModule, HttpClientModule ],
declarations: [ AppComponent, HelloComponent,
GithubFollowersComponent ],
bootstrap: [ AppComponent ],
providers: [DataService]
})
export class AppModule { }
数据服务.ts
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { catchError, map } from 'rxjs/operators';
...
constructor(private http: HttpClient) { }
getAll(){
return
this.http.get('https://api.github.com/users/IDBRAHIMDEV/followers').
pipe(map(response => response),
catchError(
this.handleError
)
)}
...
app.module.html
<github-followers></github-followers>
这里有一个工作演示:githubFollowersStackBlitz
推荐阅读
- php - 无法发布contactform.php
- node.js - Heroku 构建失败 - 推送被拒绝,无法编译 node.js 应用程序,无法缩小包,许多错误
- mysql - 创建索引后查询很快,但几分钟后查询很慢 MySQL
- javascript - MERN堆栈推送到heroku:![remote denied] master -> master (pre-receive hook denied)
- typescript - 按属性类型提取值
- android - ionic cordova build --prod --release 失败并产生错误
- docker - 无法从保存的 tar 映像加载 docker
- google-apps-script - 用表格替换搜索文本
- typescript - 如何让我的 Ionic 4 Typescript 路径正常工作?
- ruby-on-rails - Rails - 单面参考的“无法写入未知属性”