简体   繁体   English

我想在没有参数的情况下将路由设置为参数 - next.js

[英]I want to make route as params also with without params - next js

I have a NEXTJS project and I Use SSR to fetch data, but I want to make such thing:我有一个 NEXTJS 项目,我使用 SSR 来获取数据,但我想做这样的事情:

When users are on the "/" route, I show them a random product detail page, but when they search for some product and click on it, they should be redirected to eg '/product-01'.当用户在“/”路线上时,我会向他们展示一个随机的产品详细信息页面,但是当他们搜索某些产品并单击它时,他们应该被重定向到例如“/product-01”。

I know how I can take random products, don't think about that, I have a problem with routes, I want to make this route functional on one page.js file我知道如何随机抽取产品,别想了,我的路线有问题,我想让这条路线在一个 page.js 文件上起作用

finally, the question is: how can use the next.js route if the route param is just empty?最后,问题是:如果路由参数为空,如何使用 next.js 路由?

In Nextjs is you check for a router parameter if it exists then it will return you value of the param or it param does not exists then it will return undefined .在 Nextjs 中,你检查一个路由器参数,如果它存在,它会返回你的参数值,或者它不存在,然后它会返回undefined for example suppose we have product=productname then we will check router.query.product if url have that perameter than will be return productname if not than it will return undefined so you can place check of undefined to check empty parameter case.例如,假设我们有 product=productname 那么我们将检查router.query.product是否 url 有那个参数然后返回 productname 如果没有则返回 undefined 所以你可以检查undefined来检查空参数的情况。

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

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