Angular 使用角度切换页面后,页码自动重置为1
我不知道为什么bookParams中的pageNumber在我将页面从1更改为2后自动重置为1Angular 使用角度切换页面后,页码自动重置为1,angular,pagination,ngx-pagination,Angular,Pagination,Ngx Pagination,我不知道为什么bookParams中的pageNumber在我将页面从1更改为2后自动重置为1 export class BookListComponent implements OnInit { books: Book[]; bookParams: BookParams; pagination: Pagination; constructor(private bookService: BookService, private accountService: AccountSer
export class BookListComponent implements OnInit {
books: Book[];
bookParams: BookParams;
pagination: Pagination;
constructor(private bookService: BookService, private accountService: AccountService) {
accountService.currentUser$.pipe(take(1)).subscribe(user => {
this.bookParams = new BookParams(user);
console.log("contructor " + this.bookParams.pageNumber);
})
}
ngOnInit(): void {
let a = this.bookParams;
this.loadBooks();
}
loadBooks() {
console.log("loadBooks" + this.bookParams.pageNumber);
this.bookService.getBooks(this.bookParams).subscribe(response => {
this.books = response.result;
this.pagination = response.pagination;
this.pagination.currentPage = 1;
})
}
pageChanged(event: any) {
console.log("PageChange "+event.page);
this.bookParams.pageNumber = event.page;
this.loadBooks();
}
}
BookParams.ts
export class BookParams {
pageSize = 8;
pageNumber = 1;
}
点击更改页面后的图片
您的
pageChanged
函数调用您的loadbook
函数,该函数又有以下行:
this.pagination.currentPage = 1;
因此,它将在每次页面更改后指向第一页(这是假设更改页面调用
pageChanged
函数-我们没有足够的关于您所发布内容的信息。为什么每次页面更改时,您都会获取看似相同的一组书籍?谢谢,我删除了此代码,它起了作用。我在internet上学习了一门课程,但他们仍然使用this.pagination.currentPage。)=1;
而且您的it仍然有效,这非常令人困惑