【问题标题】:Why the lang parameter isn't passed down to getStaticProps from getStaticPaths?为什么 lang 参数没有从 getStaticPaths 传递给 getStaticProps?
【发布时间】:2021-03-19 16:30:55
【问题描述】:

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。 我怎样才能做到这一点?

【问题讨论】:

    标签: reactjs internationalization next.js ssg


    【解决方案1】:

    要从 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,
        };
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-20
      • 2020-01-16
      • 2017-08-02
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 2021-07-10
      • 2012-09-20
      相关资源
      最近更新 更多