【发布时间】:2020-09-20 10:31:50
【问题描述】:
我正在尝试从因 URL 而异的 API 获取数据,并尝试使用 withRouter 进行此操作,但它似乎无法在 getStaticProps 中工作。
我目前有以下代码:
export async function getStaticProps({ router }) {
const pageRequest = `http://localhost:3000/api/posts/${router.query.pid}`
const res = await fetch(pageRequest)
const json = await res.json()
return {
props: {
json,
},
}
}
它回来了:
TypeError: Cannot read property 'query' of undefined
获取 URL 中的变量以在 getStaticProps 中使用的正确方法是什么?
【问题讨论】:
标签: next.js