2

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输出:

在此处输入图像描述

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

4

1 回答 1

0

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,
    };
};
于 2021-03-20T14:29:11.790 回答