简体   繁体   English

如何使动态路由看起来像 Next.js 中的 static 路由

[英]How can make dynamic route to look like static route in Next.js

Is it possible to do show query routes as static in next.js Router.push function?是否可以在 next.js Router.push function 中将查询路由显示为 static?

I want to do something like this.我想做这样的事情。

 Router.push(
   `/statistics?entityId=${id}&entityType=Player&serviceProvider=${serviceProvider}&tab=overview`,
   `/statistics/player/id`,
   {
     shallow: true
   }

It is possible to update the route path with query params without running data-fetching methods again using shallow-routing .可以使用查询参数更新路由路径,而无需再次使用shallow-routing运行数据获取方法。 But the caveat is shallow routing will only work on the same page URL.但需要注意的是,浅层路由仅适用于同一页面 URL。 See the caveats section in the docs请参阅文档中的警告部分

So if you try to update /statistics?entityId=${id} this is only valid for the /statistics page.因此,如果您尝试更新/statistics?entityId=${id}这仅对/statistics页面有效。 You can not get the updated query param in /statistics/player/[id] page because they are two different pages.您无法在/statistics/player/[id]页面中获取更新的查询参数,因为它们是两个不同的页面。

This is valid for /statistics page这对/statistics页面有效

Router.push(
   `/statistics?entityId=${id}&entityType=Player&serviceProvider=${serviceProvider}&tab=overview`,
   { shallow: true }
)

So you can either use the shallow-routing in /statistics page and use router.query to access the updated query params then render the data based on it or you can have multiple dynamic routes like /statistics/player/[id] , /statistics/some-other-entity/[id] .因此,您可以使用/statistics页面中的浅路由并使用router.query访问更新的查询参数,然后根据它呈现数据,或者您可以拥有多个动态路由,例如/statistics/player/[id]/statistics/some-other-entity/[id]

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

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