为什么 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,};
};

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...