我想通过getStaticPaths中的当前url路径名获取最后一个数字。
http://localhost:3000/category/food/2 -> 2,
http://localhost:3000/category/food/3 -> 3,
...
我试过了。
export const getStaticPaths: GetStaticPaths = async () => {
// getAllCategories() is fetch(GET) allcategories
const allCategories = await getAllCategories() // fetch;
const num = 2; <- Get number by current url pathnames
return {
paths:
allCategories.edges.map(
({ node }: { node: { [key: string]: string } }) =>
`/category/${node.slug}/${num}`
) || [],
fallback: false,
};
};
有人知道怎么解决吗?
在我看来,你是在错误的方式,只是getStaticPaths,因为它是预渲染在构建时,你没有访问路径。
你应该把它与getStaticProps结合在一起,在那里你得到一个上下文对象来使用;详情请参阅官方文件->https://nextjs.org/docs/api-reference/data-fetching/get-static-props和https://nextjs.org/docs/api-reference/data-fetching/get-static-paths