如果你在运行的时候报错:
page.tsx doesn't have a root layout. To fix this error, make sure every page has a root layout.
说明你在使用 App Router(app/
目录)结构 的时候,说明你缺少了一个必须的文件:
你缺了:
app/layout.tsx
app/
├── layout.tsx ← 全局布局(必须)
└── page.tsx ← 首页页面,对应 /
layout.tsx
在你的 app/
目录下新建一个 layout.tsx
,比如:
// app/layout.tsx
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
你也可以加上 <head>
、导航栏、全局样式等内容。
layout.tsx
是所有页面的结构框架(比如公共导航栏、主题、全局样式);app/
├── layout.tsx ← 必须的根布局
└── page.tsx ← 首页内容
// app/layout.tsx
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
// app/page.tsx
export default function HomePage() {
return <h1>Hello, world!</h1>
}