del*_*ete 7 typescript next.js next.js13
我尝试复制/粘贴 NextJS 的官方示例,但getServerSideProps它抛出错误:
import type { InferGetServerSidePropsType, GetServerSideProps } from 'next'
type Repo = {
name: string
stargazers_count: number
}
export const getServerSideProps: GetServerSideProps<{
repo: Repo
}> = async () => {
const res = await fetch('https://api.github.com/repos/vercel/next.js')
const repo = await res.json()
return { props: { repo } }
}
export default function Page({
repo,
}: InferGetServerSidePropsType<typeof getServerSideProps>) {
return repo.stargazers_count
}
Run Code Online (Sandbox Code Playgroud)
错误:
./app/page.tsx
ReactServerComponentsError:
"getServerSideProps" is not supported in app/. Read more: https://nextjs.org/docs/app/building-your-application/data-fetching
Run Code Online (Sandbox Code Playgroud)
不幸的是,它引用了数据获取文档,其中包含导致我出现此错误的示例。
创建应用程序:
npx create-next-app@latest
(TypeScript:是,AppRouter:是,src-directory:否)
将 pages.tsx 更改为文档中的上述变体。
包.json:
"scripts": {
"dev": "next dev -p 8002",
"build": "next build",
"start": "next start -p 8002",
"lint": "next lint"
},
"dependencies": {
"@types/node": "20.4.2",
"@types/react": "18.2.15",
"@types/react-dom": "18.2.7",
"autoprefixer": "10.4.14",
"eslint": "8.45.0",
"eslint-config-next": "13.4.10",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-prettier": "^5.0.0",
"next": "13.4.10",
"postcss": "8.4.26",
"prettier": "^3.0.0",
"react": "18.2.0",
"react-dom": "18.2.0",
"tailwindcss": "3.3.3",
"typescript": "5.1.6"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^6.1.0"
}
Run Code Online (Sandbox Code Playgroud)
如何处理这个问题以及如何将 getServerSideProps 与 AppRouter 一起使用?
| 归档时间: |
|
| 查看次数: |
26891 次 |
| 最近记录: |