Here is my object from console 我的模板中有以下用于分页的代码:
<ul class="pagination-custom text-right">
<li class="pag-link"><a (click)="onClick(customers.meta.pagination.current_page - 1)"><i class="fa fa-angle-double-left"></i></a></li>
<li class="pag-link" *ngFor="let item of createRange(customers.meta.pagination.total_pages); let i = index">
<a (click)="onClick(i + 1)">{{i + 1}}</a></li>
<li class="pag-link"><a (click)="onClick(customers.meta.pagination.current_page + 1)"><i class="fa fa-angle-double-right"></i></a></li>
</ul>
这是我的函数onClick()
onClick(number) {
this.CustomersData.page(number).subscribe(
r => {this.customers = r;
});
}
这是page()
page(number) {
return this.http.get('http://127.0.0.1:8000/api/customers?page=' + (number), {headers: new HttpHeaders().set('Authorization',
this.token.t)});
}
问题是该函数运行正常,但我从控制台开始就收到以下错误:“ TypeError:无法读取未定义的属性'pagination'”
答案 0 :(得分:0)
您可以在访问密钥之前放置ternary
运算符。
<ul class="pagination-custom text-right">
<li class="pag-link"><a (click)="onClick(customers?.meta?.pagination?.current_page - 1)"><i class="fa fa-angle-double-left"></i></a></li>
<li class="pag-link" *ngFor="let item of createRange(customers.meta.pagination.total_pages); let i = index">
<a (click)="onClick(i + 1)">{{i + 1}}</a></li>
<li class="pag-link"><a (click)="onClick(customers.meta.pagination.current_page + 1)"><i class="fa fa-angle-double-right"></i></a></li>
</ul>