简体   繁体   English

如何在 NEXT JS 中的路由之间传递 state?

[英]How to pass state between routes in NEXT JS?

I have a data inside an object in my page.我的页面中的 object 中有一个数据。 I want to redirect from that page to another page along with the data like the code below我想从该页面重定向到另一个页面以及如下代码所示的数据

const redirectAppointmentStep1 = (value) => {
    router.push({
      pathname: '/Appointment/bookingstep1',
      query: {value : value},
    })
  }

and I received that data in bookingstep1 page like this我在bookingstep1页面中收到了这样的数据

 const {query} = useRouter()

but the problem is the query appear on the url and the url does not look clean.但问题是查询出现在 url 和 url 上看起来不干净。 How can I send the data from one page to another page but without appearing it on the url?如何将数据从一页发送到另一页,但不会出现在 url 上?

I am new in Next.js so any help will be highly appreciated.我是 Next.js 的新手,因此我们将不胜感激。

If you want to send "route state" you have to do it via query string, but you can actually "mask" the path that is shown in the browser via the as property.如果要发送“路由状态”,则必须通过查询字符串进行,但实际上可以通过as属性“屏蔽”浏览器中显示的路径。

Router.push路由器.push

as - Optional decorator for the URL that will be shown in the browser. as - 将在浏览器中显示的 URL 的可选装饰器。 Before Next.js Next.js之前

You can decorate the URL to match the path name.您可以装饰 URL 以匹配路径名。

const redirectAppointmentStep1 = (value) => {
  router.push({
    as: '/Appointment/bookingstep1',
    pathname: '/Appointment/bookingstep1',
    query: {value : value},
  })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM