Redirect to Previous Page In Angular

时光毁灭记忆、已成空白 提交于 2019-12-04 20:00:46

The issue you are facing is because of navigation :

pageChange(newPage: number) {
    this.router.navigate(['/books'], { queryParams: { page: newPage } });
}

You should not call this.router.navigate, instead of that directly call it like :

constructor() {
    let currentPage = localStorage.getItem('currentPage');
    this.config = {
        currentPage: currentPage ? currentPage : 1 ,
        itemsPerPage: 2
    };
}

pageChange(newPage: number) {
    localStorage.setItem('currentPage', newPage);
    this.config.currentPage = newPage;
}

I believe the problem you’re facing is because your are not using the router to store the current page. So when you click the back button on the browser it will do a router navigation and not a pagination navigation.

So instead of only updating the current page on the control, what you need to do is also store the page in the router every time a page is changed and do a navigation. This way independently if you navigate back and forth on the browser buttons the page number is always in sync.

The other advantage of using this approach is that you will be able to open the url on a new tab and start on the correct page, or even share with someone else and it will point directly on the page you are expecting, instead of resetting it to the first page.

You can check an example of implementation here, on the documentation of the ngx-pagination itself.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!