我是Next.js新手,我一直试图在我的Next.js应用程序的动态页面中使用getStaticProps,我得到了以下错误:
错误:动态SSG页面需要getStaticPaths,“/query/itMid”缺少getStaticPaths
[itmid].jsx
function Query({ posts }) {
return (
{posts.map((itm, k) => {
return (
<>
<Head>
<title> {itm.Name} - wixten </title>
</Head>
<div key={itm._id} className="Question-one">
<h1> {itm.Name}</h1>
<h2>{itm.Summary}</h2>
</div>
<div className="username">
<span className="username2">--{itm.username}</span>
</div>
</>
);
})}
</>
<div className="multi-container">
<Answershooks id={gotid} />
<RealtedPost />
</div>
</>
);
}
export async function getStaticProps() {
const res = await fetch("https://ask-over.herokuapp.com/questone/" + gotid);
console.log("check");
console.log("dada");
const posts = await res.json();
return {
props: {
posts,
},
};
}
export default Query;我为什么要犯这个错误?
发布于 2022-02-08 07:14:46
getStaticProps所做的是生成静态页面,但是您需要告诉下一个js,生成哪些路径?
export async function getStaticPaths() {
return {
paths: [
{ params: { query: 'slug-1' }},
{ params: { query: 'slug-2' }}
],
fallback: true // false or 'blocking'
};
}然后在你的getStaticProp
export async function getStaticProps({ params }) {
//params.query will return slug-1 and slug-2
const res = await fetch("https://ask-over.herokuapp.com/questone/" + params.query);
console.log("check");
console.log("dada");
const posts = await res.json();
return {
props: {
posts,
},
};
}如果您将文件命名为params.query,则需要使用[query].js。
上述代码将生成静态路径/slug-1和/slug-1。
如果您没有尝试生成静态页面(看起来是这样的),那么您可能应该使用getServerSideProps来生成页面。
https://stackoverflow.com/questions/71029092
复制相似问题