我正在尝试@
在 slug URL 前面添加。例如:https://example.com/@username
。
我试过了,@[username].js
但这不起作用。
这甚至可能吗?
我正在尝试@
在 slug URL 前面添加。例如:https://example.com/@username
。
我试过了,@[username].js
但这不起作用。
这甚至可能吗?
在您处理用户的后端,将 放入@
用户的用户名中。然后,只需将用户名视为纯文本 slug。这是有效的,因为@
在 URL 中是允许的,就像a
,b
或c
。
pages/index.js
:
import Link from 'next/link'
const username = '@foobar' // hardcoded example username, this will be recieved from your backend
export default function Index() {
return (
<Link href={username}>
<a>{username}</a>
</Link>
)
}
pages/[username].js
:
import { useRouter } from 'next/router'
export default function Username() {
const router = useRouter()
const { username } = router.query
return <h1>User: {username}</h1>
}