【发布时间】:2021-10-04 10:23:05
【问题描述】:
我遇到了构建时错误,错误:动态 SSG 页面需要 getStaticPaths 并且“/category/[...Id]”缺少 getStaticPaths,如何在我的代码中使用 getStaticPaths 引用https://nextjs.org/docs/messages/invalid-getstaticpaths-value this 但是如何在我的函数中调用路径,你能解决我的问题吗
代码 [...Id].js
import { useRouter } from 'next/router'
import Head from 'next/head'
import { Grid, Image,Segment, Card } from "semantic-ui-react"
import 'semantic-ui-css/semantic.min.css'
import Layout from '../../components/layout'
const Post = (props) => {
const router = useRouter()
const { Id } = router.query
console.log(props.category_list)
return(
<>
<Layout>
<Head>
<meta charSet="utf-8" />
<title></title>
{/* <meta name="description" content={seo_description} /> */}
<meta name="keywords" content=""/>
<meta name="google-site-verification" content="rtIRrUNRpgZ_lFtlfS8LJ0-8j_d569BXS9NOGa_nM6Y" />
</Head>
<Grid className="store-list">
<Grid.Column width={16}>
<p>
<span>{props.category_title.heading_label}</span>
</p>
</Grid.Column>
</Grid>
<Grid columns={4} className="all-offers storeList">
{props.category_list.map((x) => {
return (
<Grid.Column>
<Segment>
<Card>
<a href ={x.store_url}>
{" "}
<Image
src={x.image}
alt=""
className="collection-img"
></Image>
</a>
<Card.Content>
<a href ={x.store_url}>
{" "}
<Card.Header>{x.name}</Card.Header>
</a>
<Card.Description>{x.store_summary}</Card.Description>
</Card.Content>
<Card.Content extra>
<p className="rewards">
<span>
<img src="/images/rewards.png" alt=""></img>
</span>
Cash rewards upto <span>AED {x.cashback}</span>
</p>
<p className="location">
<span>
<img src="/images/location.png" alt=""></img>
</span>
<span className="store-location" key="index">{x.store_branches}</span>
{/* {x.store_branches.map((locations, index) => (
<span className="store-location">
{index === 0 ? (
<span>{locations.store_location}</span>
) : index >= 1 ? (
<span>
, {locations.store_location}
</span>
) : null}
</span>
))} */}
</p>
</Card.Content>
</Card>
</Segment>
</Grid.Column>
);
})}
</Grid>
</Layout>
</>
)
}
export async function getStaticProps(context) {
const id = context.params.Id[1];
const postBody = {
category_id: id,
offer_type: "",
};
const offerList = await fetch('https://le.app.ae/api/v5/web/list',{
method:'POST',
body: JSON.stringify(postBody),
headers: { "Content-Type": "application/json" },
})
const category = await offerList.json();
// const bookJson = JSON.stringify(book)
// const bookJson=offerData.stores;
const category_list=category.stores;
const category_title=category;
return {
props: {
category_list,
category_title
}
};
}
export default Post;
【问题讨论】:
-
我的分类链接是localhost:3000/category/26
-
你能帮我一个忙吗?