用Angular切换页面后页码自动重置为1

问题描述 投票:0回答:2

我不知道为什么当我将页面从 1 更改为 2 后,bookParams 中的 pageNumber 自动重置为 1。

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;
}

点击更改页面后的图片

angular pagination ngx-pagination
2个回答
1
投票

您的

pageChanged
函数调用您的
loadBooks
函数,该函数又包含以下行:

this.pagination.currentPage = 1;

因此,每次页面更改后它将指向第一页(假设更改页面调用

pageChanged
函数 - 我们对您发布的内容没有足够的信息。


0
投票

作为业务用户/报告_对于业务用户和报告 - 单击不同页码时创建通知的顺序会发生变化

© www.soinside.com 2019 - 2024. All rights reserved.