错误:在 /[locales] 的 generateStaticParams 中未以字符串形式提供必需的参数(区域设置)

Error: A required parameter (locales) was not provided as a string in generateStaticParams for /[locales]

提问人:Teshane Crawford 提问时间:2/16/2023 更新时间:3/9/2023 访问量:258

问:

我正在尝试使用 generateStaticParams 为我的 Next.js 13 应用程序生成一些静态参数,但我不断收到以下错误:

“错误:在 /[locales] 的 generateStaticParams 中未提供必需的参数(区域设置)作为字符串”

以下是相关代码:

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>
    );
}
javascript reactjs typescript 参数传递 next.js13

评论


答:

1赞 Marlon Schlosshauer 3/9/2023 #1

您的参数必须完全匹配。在这种情况下,您的文件夹被命名为(复数),您的参数返回(单数)。localeslocale

export function generateStaticParams(): { params: { locales: string } }[] {
    return [{ params: { locales: 'en' } }, { params: { locales: 'de' } }];
}