小编DrJ*_*lik的帖子

NextJS中的静态分页

我正在尝试在 Next JS 中设置分页,但我无法弄清楚如何通过 getStaticProps 实现这一点。我可以通过带有查询参数的 getServerSideProps 来做到这一点,但这不能通过 getStaticProps 访问。数据来自本地 Strapi 后端。

这是 getServerSideProps 的示例(有效):

export async function getServerSideProps({ query: { page = 1 } }) {
const { API_URL } = process.env;

const start = +page === 1 ? 0 : (+page - 1) * 3;
const numberOfCakesRes = await fetch(`${API_URL}/cakes/count`);
const numberofCakes = await numberOfCakesRes.json();

const res = await fetch(`${API_URL}/cakes?_limit=3&_start=${start}`);
const data = await res.json();

return {
    props: {
        cakes: data,
        page: +page,
        numberofCakes,
    },
};
Run Code Online (Sandbox Code Playgroud)

}

然后我只需将按钮连接到路由器即可来回切换。 …

javascript next.js

6
推荐指数
1
解决办法
5974
查看次数

标签 统计

javascript ×1

next.js ×1