gpt4 book ai didi

javascript - 在 NextJS 路由中的 slug 前面添加符号(例如@)

转载 作者:行者123 更新时间:2023-12-04 17:20:38 34 4
gpt4 key购买 nike

我正在尝试在 slug URL 前添加 @。例如:https://example.com/@username

我尝试了 @[username].js 但它不起作用。

这可能吗?

最佳答案

尝试使用 rewrites和一些正则表达式。 Next.js 使用 path-to-regexp在幕后。

  async rewrites() {
return [
{
source: '/:userId(@[a-zA-Z0-9]+)/:id*',
destination: "/user/:userId/:id*",
}
]
}

在客户端使用next/link:

<Link href={`@${userId}`}>
<a>user</a>
</Link>

关于javascript - 在 NextJS 路由中的 slug 前面添加符号(例如@),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66389057/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com