@77io/pagination 中文文档教程

发布于 3年前 浏览 25 项目主页 更新于 3年前

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();
});
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文