@77io/pagination 中文文档教程
Pagination
分页使分页 api 易于使用。 它返回一个发出每个页面的 Observable。
给定基本 url 和(可选)标题,可以轻松地以分页样式获取记录。
这适用于任何支持 link 标头的 api。
从 Wordpress api 抓取所有帖子的示例。
import Pagination from "@77io/pagination";
import { last, reduce } from "rxjs/operators";
// Setup the basic options
const baseUrl = wordpressRoot + "/wp-json/wp/v2/posts";
const headers = {
accept: "application/json"
};
const sub = Pagination(baseUrl, headers)
.pipe(
reduce((arr: any[], page: any[]) => {
return arr.concat(page);
}, []),
last()
)
.subscribe((allPosts: any[]) => {
console.log("I got all posts!");
sub.unsubscribe();
});
从 Wordpress api 获取第一页的示例。
import Pagination from "@77io/pagination";
// Setup the basic options
const baseUrl = wordpressRoot + "/wp-json/wp/v2/posts";
const headers = {
accept: "application/json"
};
const sub = Pagination(baseUrl, headers).subscribe((posts: any[]) => {
console.log("I got the first page of posts!");
sub.unsubscribe();
});
Pagination
Pagination makes paginated apis easy to work with. It returns an Observable that emits each page.
Given a base url and (optional) headers, easily grab records in a paginated style.
This works with any api that supports the link header.
An example grabbing all of the posts from the Wordpress api.
import Pagination from "@77io/pagination";
import { last, reduce } from "rxjs/operators";
// Setup the basic options
const baseUrl = wordpressRoot + "/wp-json/wp/v2/posts";
const headers = {
accept: "application/json"
};
const sub = Pagination(baseUrl, headers)
.pipe(
reduce((arr: any[], page: any[]) => {
return arr.concat(page);
}, []),
last()
)
.subscribe((allPosts: any[]) => {
console.log("I got all posts!");
sub.unsubscribe();
});
An example getting the first page from the Wordpress api.
import Pagination from "@77io/pagination";
// Setup the basic options
const baseUrl = wordpressRoot + "/wp-json/wp/v2/posts";
const headers = {
accept: "application/json"
};
const sub = Pagination(baseUrl, headers).subscribe((posts: any[]) => {
console.log("I got the first page of posts!");
sub.unsubscribe();
});