【问题标题】:How to send data to another NextJS page?如何将数据发送到另一个 NextJS 页面?
【发布时间】:2021-08-21 09:32:50
【问题描述】:

我想基于输入搜索。

能够将内容发送到另一个页面,然后该页面使用输入搜索的内容发出 API 请求。

基本上,我希望用户能够使用搜索栏,输入“某物”,然后这个“某物”可以用来发出 API 请求。

我的文件:

- index.js

- search

  - q.js

index.js:

...

form action="./search/q" method="get">
  <button type="submit">
    <i class="fas fa-search fa-2x"></i>
  </button>
  <input type="text" placeholder="Search" id="query" name="serach" />
</form>

q.js:

import Head from "next/head";

export default function Home({ articles }) {
  return (
    <>
      <Head>
        <title>Test</title>
      </Head>
      <main class="container">...</main>
    </>
  );
}

export async function getStaticProps() {
  const articles = await fetch(`...${query}`).then((r) => r.json());
  return {
    props: {
      articles,
    },
  };
}

【问题讨论】:

    标签: javascript html next.js


    【解决方案1】:

    你这样做的方式是不可能的。

    您可以使页面服务器端呈现,将数据作为查询参数发送,然后您可以在 getServerSideProps 中访问路由器(有查询参数)。如果你想制作静态页面,那么你可以在客户端获取数据。

    【讨论】:

    猜你喜欢
    • 2021-04-15
    • 2020-08-16
    • 2014-05-15
    • 1970-01-01
    • 2013-01-24
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多