參考代碼
示例數(shù)據(jù)
在 NextJS 項(xiàng)目下添加文件 ./data/user.json
,示例內(nèi)容如下
{
"name": "Anoyi??"
}
示例讀取文件數(shù)據(jù)
添加頁(yè)面 ./pages/user.tsx
,示例內(nèi)容如下
import { readFileSync } from 'fs'
import path from "path"
const Page = ({user}) => {
return (
<div>
{user.name}
</div>
)
}
export async function getStaticProps() {
const user = readFileSync(path.join(process.cwd(), 'data', `user.json`), 'utf-8')
return {
props: {
user: JSON.parse(user),
},
}
}
export default Page