所以我有 getStaticProps 并且需要根据变量获取一些数据这里是一个例子
export async function getStaticProps() {
const res = await fetch(localWordpressUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
query: `
query AllPostsQuery {
posts(where: {categoryName: "blog"}) {
nodes {
slug
content
title
}
}
}
`,
})
});
const json = await res.json();
return {
props: {
posts: json.data.posts,
}
};
}
其中 categoryName: "blog"需要是一个变量而不是硬编码。我知道你可以得到鼻涕虫,但我需要的是鼻涕虫之前。即 site.com/blog/slug。对此有何建议?
最佳答案
你真的很接近。您需要的是从 getStaticProps
函数中获取上下文。将其添加到您的 getStaticProps
函数,然后查看控制台日志
以了解发生了什么。
export async function getStaticProps(context) {
console.log(JSON.stringify(context, null, 3));
const { asPath, req, res, pathname, query } = context;
if (req) {
let localWordpressURL = req.headers.host;
console.log(localWordpressURL);
}
}
https://stackoverflow.com/questions/72101692/
相关文章:
flutter - 从 2.8.1 升级到 Flutter 3.0,得到 : Warning: Op
amazon-web-services - 如何找出弹性 beantalk 部署失败的原因?
node.js - 如何在工作区中使用 npm-shrinkwrap?
google-cloud-platform - 为什么 Airflow PubSubPullOper
azure - 我正在将 AddMicrosoftIdentityWebApi .net core
reactjs - 如何让 Next Image 组件与 Storybook 一起工作