当我使用 httpClient 调用Web服务时,我开始在我的角度服务中使用Spring Rest和Angular 5实现简单的分页,并获得正确的响应,请求的页面显示了数据我的页面正确无误,但控制台Web浏览器出现错误,尽管模板显示正确,但 * ngFor 循环中没有读取undefined属性:
问题出在Service方法中:getPageClient()和* ngFor指令 这是我的错误日志:
这是我使用可观察的服务:
import { Injectable } from '@angular/core';
import {Client} from '../models/Client'
import {PageClient} from '../models/PageClient'
import { Observable, of } from 'rxjs';
import { HttpClient,HttpHeaders } from '@angular/common/http';
import {catchError,map,tap} from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class ClientService {
private url = 'http://localhost:8099/api/clients';
private urlPage = 'http://localhost:8099/api/clients/get?page=0&size=3';
getClient(): Observable<Client[]>{
return this.http.get<Client[]>(this.url)
.pipe(
catchError(this.handleError('getClient', []))
);
}
getPageClient(): Observable<PageClient>{
return this.http.get<PageClient>(this.urlPage)
.pipe(
map(response => {
const data = response;
console.log(data.content);
return data ;
}));
}
constructor(private http : HttpClient) { }
private handleError<T> (operation = 'operation', result?: T) {
return (error: any): Observable<T> => {
// TODO: send the error to remote logging infrastructure
console.error(error); // log to console instead
// TODO: better job of transforming error for user consumption
console.log(`${operation} failed: ${error.message}`);
// Let the app keep running by returning an empty result.
return of(result as T);
};
}
}
这是我的模特课:
import {Client} from '../models/Client' ;
export class PageClient {
content : Client[];
totalPages : number;
totalElements : number;
last : boolean;
size : number ;
first : boolean ;
sort : string ;
numberOfElements : number ;
}
这是我的组件代码:
import { Component, OnInit } from '@angular/core';
import {Client} from '../models/Client' ;
import {PageClient} from '../models/PageClient'
import {ClientService} from '../services/client.service' ;
@Component({
selector: 'app-clients',
templateUrl: './clients.component.html',
styleUrls: ['./clients.component.css']
})
export class ClientsComponent implements OnInit {
clients : Client[];
pageClient : PageClient ;
getClient(): void {
this.clientService.getClient()
.subscribe(clients => this.clients = clients);
}
getPageClient(): void {
this.clientService.getPageClient()
.subscribe(page => this.pageClient = page);
}
constructor(private clientService : ClientService) { }
ngOnInit() {
this.getClient();
this.getPageClient();
}
}
这是我实现分页的模板部分:
<nav aria-label="...">
<ul class="pagination" *ngIf="pageClient.content" >
<li class="page-item disabled">
<a class="page-link" href="#" tabindex="-1">Previous</a>
</li>
<li *ngFor="let page of pageClient.content ; let i=index " class="page-item"><a class="page-link" href="#">{{i}}</a></li>
<!-- <li class="page-item active">
<a class="page-link" href="#">2 <span class="sr-only">(current)</span></a>
</li>-->
<li class="page-item">
<a class="page-link" href="#">Next</a>
</li>
</ul>
</nav>
有人可以帮助我发现我的Observable问题吗 预先感谢
答案 0 :(得分:2)
由于getPageClient
函数是异步的,因此在首次加载页面时pageClient
是未定义的。这意味着在进行pageClient.content
时,您会得到一个错误。
非常感谢,Angular提供了一些有用的语法,您可以在模板中使用以避免这种情况。您应该改用*ngIf="pageClient?.content" >
。
如果?
不为null或未定义,content
告诉Angular仅读取pageClient
。