为什么 lang 参数没有从 getStaticPaths 传递给 getStaticProps?

问题描述

getStaticPaths 方法

export const getStaticPaths: GetStaticPaths = async () => {
  let ed = await fetch(`${baseURL}getEvents2`,{
    method: "post",});
  let events = await ed.json();
  const paths = ["hu","en"].flatMap((lang) =>
    events.map((eventId) => ({
      params: { lang: lang,eventId: eventId },}))
  );
  return {
    paths,fallback: true,};
};

getStaticProps

export const getStaticProps: GetStaticProps = async ({ ...context }) => {
  console.log(context);
}

console.log 输出

enter image description here

我想在上下文中以某种方式看到 lang。 我怎么能做到这一点?

解决方法

要从 getStaticPaths 返回要在 getStaticProps 中呈现的区域设置变体,您应该使用路径对象中的 locale 字段。

export const getStaticPaths: GetStaticPaths = async () => {
    let ed = await fetch(`${baseURL}getEvents2`,{
        method: "post",});
    let events = await ed.json();
    const paths = ["hu","en"].flatMap((lang) =>
        events.map((eventId) => ({
            params: { eventId: eventId },locale: lang // Pass `locale` here
        }))
    );
    return {
        paths,fallback: true,};
};