【问题标题】:How to handle navigation using react-static and contentful API如何使用 react-static 和内容 API 处理导航
【发布时间】:2021-09-06 01:02:00
【问题描述】:

我在我的应用程序和内容 API 中使用 react-static 来加载我的数据。我有一个ebooks 数组,每个数组元素都有一个redirectUrl 字段,该字段指向一个包含该元素的pdf 的新页面(另一个名为pdf 的数组的一部分)。 ebooks 数组中的每个元素都是一个页面,其中包含一个按钮,该按钮重定向到包含pdf 数组中相应元素的redirectUrl

if (resp.status === 200) {
      window.location.href = `/${
      eBook.fields.redirectUrl
   }`;

其中eBook 是数组eBooks 的一个元素。我已经像这样设置了static.config.js 文件

let eBooks = await client.getEntries({
      content_type:"ebook",
      order: "-sys.createdAt",
    })
    .then((response) => response.items)
      .catch(console.error);
    
    let pdf = await client.getEntries({
      content_type:"pdf",
      order: "-sys.createdAt",
    })
    .then((response) => response.items)
      .catch(console.error);

在return语句中

{
        path: "/",
        getData: () => ({
          eBooks,
        }),
        children: eBooks.map((eBook) => ({
          path: `/${urlBuilder(
            eBook.fields.url ? eBook.fields.url : eBook.fields.title
          )}`,
          template: "src/pages/embedded-finance-101",
          getData: () => ({
            eBook,
          }),
        })),
      },
      {
        path: `/${urlBuilder(
          eBook.fields.redirectUrl
        )}`,
        template: "src/pages/pdf-viewer",
        getData: () => ({
          pdf,
        })
      },

在提交时,我被重定向到正确的 url,即{eBook.fields.redirectUrl},但页面是空的,而不是 pdf-viewer。我哪里错了。

【问题讨论】:

    标签: javascript reactjs contentful react-static


    【解决方案1】:

    我在内容模式中设置内容模式的方式存在问题。修复后它就可以工作了。

    【讨论】:

      猜你喜欢
      • 2016-10-27
      • 1970-01-01
      • 2020-01-18
      • 2018-01-01
      • 1970-01-01
      • 2021-04-28
      • 2021-09-13
      • 2021-12-02
      • 1970-01-01
      相关资源
      最近更新 更多