我正在制作一个只有两页的简单 Next Js 应用程序。
索引.tsx:
import React from "react";
import Link from "next/link";
export default function Index() {
return (
<div>
<Link
href={{
pathname: "/about",
query: { candidateId: 8432 }
}}
as="about"
>
Go to the about page
</Link>
</div>
);
}
根据上面的代码,单击Go to the about page
它会转到 about 页面并使用query
I 还接收有关页面中传递的查询值。
关于.tsx
import React from "react";
import Router, { withRouter } from "next/router";
function About({ router: { query } }: any) {
return (
<div>
Candidate Id: <b> {query.candidateId} </b>
</div>
);
}
export default withRouter(About);
这会显示该值,但在我们处于页面时刷新/about
页面时,candidateId
接收到的值消失了。
要求:即使在页面刷新时,也请帮助我保留从一个页面传递到另一个页面的查询值。
注意:根据我的要求,我不应该canidateId
在导航时显示on url,因此我正在使用as
方法..我知道如果我删除我可以实现它as
但我无法在导航时在索引页面中删除它..原因是这会导致在不打算的 url 中显示CandidateId..
尝试了这个解决方案:https : //stackoverflow.com/a/62974489/7785337但这在页面刷新时给出了空的查询对象。
卡住了很长时间,请帮助我。