英文:
Error: A required parameter (locales) was not provided as a string in generateStaticParams for /[locales]
问题
我正在尝试使用 generateStaticParams 为我的 Next.js 13 应用程序生成一些静态参数,但我一直遇到以下错误:
"错误:在 /[locales] 的 generateStaticParams 中未提供必需的参数 (locales) 作为字符串"
以下是相关代码:
英文:
I am trying to generate some static parameters for my Next.js 13 application using generateStaticParams, but I keep getting the following error:
"Error: A required parameter (locales) was not provided as a string in generateStaticParams for /[locales]"
Here is the relevant code:
import * as React from 'react';
import { NextIntlClientProvider } from 'next-intl/client';
import { notFound } from 'next/navigation';
export function generateStaticParams(): { params: { locale: string } }[] {
return [{ params: { locale: 'en' } }, { params: { locale: 'de' } }];
}
export default async function LocaleLayout({
children,
params: { locale },
}: {
children: React.ReactNode;
params: { locale: string };
}) {
if (typeof locale !== 'string') {
return notFound();
}
let messages;
try {
messages = (await import(`@/messages/${locale}.json`)).default;
} catch (e) {
return notFound();
}
return (
<html lang={locale}>
<body>
<NextIntlClientProvider locale={locale} messages={messages}>
{children}
</NextIntlClientProvider>
</body>
</html>
);
}
答案1
得分: 1
你的参数必须完全匹配。在这种情况下,您的文件夹名为 locales
(复数形式),而您的参数返回 locale
(单数形式)。
export function generateStaticParams(): { params: { locales: string } }[] {
return [{ params: { locales: 'en' } }, { params: { locales: 'de' } }];
}
英文:
Your parameters must match exactly. In this case your folder is named locales
(plural) and your params return locale
(singular).
export function generateStaticParams(): { params: { locales: string } }[] {
return [{ params: { locales: 'en' } }, { params: { locales: 'de' } }];
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论