如何在下一个路由器中传递多个查询

Dem*_*n37 2 reactjs react-router next.js next-router

我正在 next.js 和 next-router 工作

我有 2 个要传递的数据参数

一个是entity_id,另一个是url_key。

data={
    entity_id: 5,
    url_key: 'canada/ontario/store_five'
}
Run Code Online (Sandbox Code Playgroud)

目前我可以传递一个 url_key:

 Router.push('/store?url_key=' + marker.url_key, `/store/${marker.url_key}`)
Run Code Online (Sandbox Code Playgroud)

URL 正如我想要的那样出现

http://BaseUrl/store/canada/ontario/store_five
Run Code Online (Sandbox Code Playgroud)

现在我还想将entity_id 与上面的url_key 一起发送,但不应显示在 URl 中

fel*_*osh 5

您可以根据需要传递任意数量的查询参数,只需使用查询字符串即可。

// using urls
Router.push(
  `/store?url_key=${marker.url_key}&entity_id=${marker.entity_id}`,
  `/store/${marker.url_key}`
);

// using object
Router.push({
  pathname: '/store',
  query: { url_key: marker.url_key, entity_id: marker.entity_id },
  asPath: `/store/${marker.url_key}`,
});
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请阅读路由器文档