简体   繁体   English

Next.js 添加 slug/params 以清洁 URL

[英]Next.js Add slug/params to clean URL

I have a structure with a clean URL in the "pages" folder like this...我在“页面”文件夹中有一个干净的 URL 结构,如下所示......

/pages/items/[pageNumber]/index.tsx

And if I use router.push I use it like this to get to that page...如果我使用router.push ,我会像这样使用它来访问该页面......

router.replace("/items/[pageNumber]", "/items/1")

That works great, but now I want to add a query params (for filter) to that URL, how can I do that?这很好用,但现在我想向 URL 添加一个查询参数(用于过滤器),我该怎么做?

I tried this...我试过这个...

router.push(`/items/[pageNumber]/`, `/items/1/?${filterParams}`, { shallow: true });

But that doesn't work, and I'm wondering how to do that.但这不起作用,我想知道如何做到这一点。

When you use it like this当你像这样使用它时

router.push(`/items/[pageNumber]/`, `/items/1/?${filterParams}`, { shallow: true });

you are not passing query params to the page, you are only showing them in the address bar, I guess.你没有将查询参数传递给页面,我猜你只是在地址栏中显示它们。

How about something like this:像这样的东西怎么样:

router.push({
  pathname '/items/[pageNumber]/',
  query: {
    param1: 'yes',
  },
}, {
  pathname: '/items/1/',
  query: {
    param1: 'yes',
  },
});

Don't forget to update query part for your case.不要忘记为您的案例更新query部分。

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

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