小编Sou*_*ngh的帖子

如何在不使用 getStaticPaths 的情况下从 getStaticProps 中的 url 获取参数?

下面是我的函数,其中我使用 getServerSideProps() 方法并根据我的 URL 中的参数 post_slug 动态获取数据。

// This gets called on every request
export async function getServerSideProps({ query }) {
  const { post_slug } = query || {};

  let url = API_URLS.POST.GET_POST_BY_SLUG;

  url = url.replace(/#POST_SLUG#/g, post_slug);

  const res = await fetch(url);
  const { data } = (await res.json()) || {};

  // Pass post_data to the page via props
  return { props: { data } };
}
Run Code Online (Sandbox Code Playgroud)

但是这个函数在每个请求上渲染一个完整的页面。

所以现在我决定使用 getStaticProps() 但在这里我无法从 URL 获取参数 post_slug。

我阅读了下一个 js文档,他们告诉我需要使用 getStaticPaths() 和 …

reactjs next.js

3
推荐指数
3
解决办法
9253
查看次数

标签 统计

next.js ×1

reactjs ×1