GROK

既然你选择 Next.js,我为你设计一个简单的学习路径:

  1. 基础:页面和路由

    • pages/ 下创建新页面,如 about.tsx
      export default function About() {
        return <h1>About Page</h1>;
      }
      
    • 访问 http://localhost:3000/about
  2. 动态路由

    • 创建 pages/post/[id].tsx
      import { useRouter } from 'next/router';
      
      export default function Post() {
        const router = useRouter();
        const { id } = router.query;
        return <h1>Post ID: {id}</h1>;
      }
      
    • 访问 http://localhost:3000/post/123
  3. 组件和状态

    • components/ 下创建 Counter.tsx
      import { useState } from 'react';
      
      export default function Counter() {
        const [count, setCount] = useState(0);
        return (
          <div>
            <p>Count: {count}</p>
            <button onClick={() => setCount(count + 1)}>Add</button>
          </div>
        );
      }
      
    • pages/index.tsx 中引入:
      import Counter from '../components/Counter';
      
      export default function Home() {
        return (
          <div>
            <h1>Welcome to Next.js!</h1>
            <Counter />
          </div>
        );
      }
      
  4. API 路由

    • 创建 pages/api/hello.ts
      import { NextApiRequest, NextApiResponse } from 'next';
      
      export default function handler(req: NextApiRequest, res: NextApiResponse) {
        res.status(200).json({ message: 'Hello from API!' });
      }
      
    • 访问 http://localhost:3000/api/hello

自学路径

进阶

究极😅