在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)