小编sak*_*lan的帖子

Next.js ISR 将附加数据从 getStaticPaths 传递到 getStaticProps

SingleBlogPost.jsx中我有:

export async function getStaticPaths() {
  const res = await fetch("http://localhost:1337/api/posts");
  let { data } = await res.json();

  const paths = data.map((data) => ({
    params: { slug: data.attributes.slug },
  }));

  return {
    paths,
    fallback: "blocking",
  };
}
Run Code Online (Sandbox Code Playgroud)

我通过他们的slug 生成博客页面。但是在 getStaticProps 中,我需要通过 slug 获取单个帖子,但我想通过 id 来获取。

export async function getStaticProps(context) {
  console.log("context", context);

  const { slug } = context.params;

  console.log("slug is:", slug);
  const res = await fetch("http://localhost:1337/api/posts");
  const { data } = await res.json();

  return {
    props: {
      data, …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs next.js

9
推荐指数
1
解决办法
2992
查看次数

标签 统计

javascript ×1

next.js ×1

reactjs ×1