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

huangapple go评论51阅读模式
英文:

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' } }];
}

huangapple
  • 本文由 发表于 2023年2月16日 10:36:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467292.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定