我知道nextjs可以有[id].jsx
這個頁面,但是只能是數字,比如說localhost/post/34
,請問是否有辦法可以做出像是github profile一樣,像是https://github.com/myaccount
,類似這樣子。
可以參考一下 NextJS Dynamic Routing
如果要像是 Github 那樣可以從根目錄建立一個動態路徑
檔案長這樣:pages/[id].js
=> https://github.com/Gary50613
如果希望使用者名稱後面還有東西可以用資料夾的方式pages/[id]/settings.js
=> https://github.com/Gary50613/settings
https://nextjs.org/docs/routing/introduction
感覺是只要在 pages 下面創一個叫 myaccount 的 file?
import React from 'react';
const UserProfile = ({ username }) => {
return (
<div>
<h1>Hello {username}!</h1>
</div>
);
};
UserProfile.getInitialProps = async ({ query }) => {
return { username: query.username };
};
export default UserProfile;
然後,您可以使用以下 URL 訪問該頁面:
http://localhost:3000/myaccount