# 错误：不允许使用布局段配置 'X'。请改用 generateMetadata 或 generateStaticParams。

- **ID:** `nextjs/layout-segment-config-export`
- **领域:** nextjs
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

在 Next.js app 目录中，布局段配置（如 'layout' 或 'template'）不能直接导出；必须替换为正确的数据获取或元数据 API。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| next@13.5.0 | active | — | — |
| next@14.0.0 | active | — | — |
| next@14.1.0 | active | — | — |

## 解决方案

1. ```
   将布局段配置替换为 generateMetadata。例如，如果你导出了 'metadata'，改为：

export async function generateMetadata({ params }) {
  const data = await fetchData(params.slug);
  return { title: data.title };
}
   ```
2. ```
   如果配置用于静态生成，使用 generateStaticParams：

export async function generateStaticParams() {
  const posts = await fetchPosts();
  return posts.map(post => ({ slug: post.slug }));
}
   ```
3. ```
   对于动态段，确保布局有 default.tsx 或 loading.tsx 来处理回退状态。
   ```

## 无效尝试

- **** — The layout still needs to define metadata or static params; removing the export breaks the page functionality. (70% 失败率)
- **** — Next.js only recognizes specific exports; arbitrary names are ignored and cause build errors. (90% 失败率)
- **** — Client components cannot use metadata or static params; this creates a different error. (80% 失败率)
