javascript - 如何将数据发送到另一个 NextJS 页面?
问题描述
我想基于输入搜索。
为了能够将内容发送到另一个页面,然后该页面使用输入搜索的内容发出 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,
},
};
}
解决方案
你这样做的方式是不可能的。
您可以使页面服务器端呈现,将数据作为查询参数发送,然后您可以在 getServerSideProps 中访问路由器(有查询参数)。如果要制作静态页面,则可以在客户端获取数据。
推荐阅读
- c# - 关联/链接单个表中的记录
- android - android/kotlin:Material Button 显示一个灰色框,然后该框消失
- javascript - 如何更改传单簇中的标记颜色?
- st - 是否可以在 ST 中查询方法的特定输入变量?
- javascript - 返回对象数组中的对象数组中的项
- python - 密度函数之和(基于直方图)不等于1
- php - Laravel:如何配置 eloqent 以在数据库超时时抛出异常?
- scala - Spark 2.4 的 lineSep 选项
- python-3.x - 使用 TDOA 技术计算位置坐标
- javascript - {Better Discord Plugin} 用于电子邮件和 IP 记录代码 Js 和 Fetch