1 回答

TA貢獻1871條經驗 獲得超8個贊
您的 html 代碼很好,然后在組件中聲明 matPaginator 并訂閱可觀察的頁面:
import { MatPaginator } from '@angular/material/paginator';
export class TopicsComponent implements OnInit, AfterViewInit {
form: FormGroup;
topics: Topics[];
Math: Math = Math;
@ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
constructor(
public forumService: ForumService,
public dataService: DataserviceService,
public metaService: Meta,
public titleService: Title) {
}
ngOnInit() {
//take just one value from the getTopics() for the first page
this.forumService.getTopics().pipe(take(1)).subscribe((topics: Topics[]) => {
this.topics = topics;
console.log(this.topics);
});
this.titleService.setTitle("Forum | New World Hub");
this.metaService.updateTag({ name: 'description', content: 'Guides for every aspect of Amazons game New World'});
}
//the viewChild decorator return only in the ngAfterViewInit method
ngAfterViewInit(){
this.paginator.page.pipe(
switchMap(() => {
// do whatever with the current page size and page index
const pageIndex = this.paginator.pageIndex
const pageSize = this.paginator.pageSize
// run getTopics() to your service with the current page index(make sure your functions supports it)
return this.getTopics({page: pageIndex})
})
).subscribe((topics) => {
this.topics = topics
})
}
}
- 1 回答
- 0 關注
- 135 瀏覽
添加回答
舉報