angular - 角度分页作为不同组件的问题
问题描述
我为我的 Angular Web 应用程序创建了一个分页,它工作得很好。但是我多次重复相同的代码,每次针对不同的页面/组件,这是不好的编码,所以我创建了一个新的分页组件。
然后,而不是为每个 html 文件添加这个块 -
<pagination-template #p="paginationApi" [id]="config.id" (pageChange)="config.currentPage = $event">
<div class="custom-pagination">
<button class="previous" [class.disabled]="p.isFirstPage()"
*ngIf="!p.isFirstPage()" (click)="previous()">previous</button>
<div class="page-number" *ngFor="let page of p.pages" [class.current]="p.getCurrent() === page.value">
<button class="not-current-page" (click)="onPageChange(page.value)"
*ngIf="p.getCurrent() !== page.value">{{ page.label }}</button>
<button class="current-page" *ngIf="p.getCurrent() === page.value">{{ page.label }}</button>
</div>
<button class="next" [class.disabled]="p.isLastPage()" *ngIf="!p.isLastPage()"
(click)="next()">next</button>
</div>
</pagination-template>
我只是写了
<app-pagination></app-pagination>
,但它没有用。
这些按钮是可见的,但它们不会更改页面,它们只是将我带到当前页面的顶部。
这是 .ts 文件:
export class PaginationComponent implements OnInit {
things: Thing[];
searchInput = '';
p: number = 1;
config = {id: 'custom', maxSize: 3, itemsPerPage: 4, currentPage: 1};
constructor(private router: Router, private activatedRoute: ActivatedRoute) { }
ngOnInit() {
window.scrollTo(0, 0);
this.activatedRoute.data.subscribe((data: { things: Thing[] }) => {
this.things = data.things.slice().reverse();
});
}
onPageChange(event) {
this.config.currentPage = event;
window.scrollTo(0, 0);
}
previous() {
this.config.currentPage--;
window.scrollTo(0, 0);
}
next() {
this.config.currentPage++;
window.scrollTo(0, 0);
}
}
感谢 :)))
解决方案
推荐阅读
- mongodb - Docker MongoDB shell 脚本在连接时失败
- javascript - 为什么我页面中的一个选项卡没有像其他选项卡一样提供反馈?
- python - Python: Reason for the name parameter when defining a type or named tuple
- javascript - Node.js - 将大量 PNG 图像读入数组的最佳选择?
- python - 为什么我在解析数据帧时会收到错误,但当它是单行时却没有?
- android - 如何通过firebase实时数据库在android studio的textview中指定其中一个来从一组数据中检索特定数据?
- react-native - undefined 不是 react native 中的对象(评估'g.ThemeColors.light'),expo.io
- c# - 如何在 xaml 中显示具有特定有效小数的货币?
- python-3.x - Python从文本中解析某些单词
- flutter - 我如何访问我的 API 请求类(无小部件类)中的 bloc