Create New Route In Remix
在 Remix 中有個 routes 的資料夾負責處理 route,有幾種新增 route 的方式
-
routes/dashboard.tsx
-
routes/dashboard/route.tsx
-
routes/dashboard._index/route.tsx
-
routes/dashboard._index.tsx
這幾種方式都是建立 /dashboard 這個 route,另外 Remix 本身有個 root.tsx 的檔案是處理 layout,當然也可以建立專屬 dashboard 的 layout,但是如果我們用資料夾的架構去建立 route 時,就沒辦法建立接著 dashboard 這個 slug 之後的 layout , 再來 Remix 只認 routes 底下第一層資料夾當作路徑,更深巢狀的資料夾就不會被當成路徑,比如說 /dashboard/chart 這邊如果按照上述的 2 的做法的話應該要是 routes/dashboard/chart/route.tsx
但是這是不會被當成一個可使用的路徑,因此我還是使用扁平化的路徑管理,當需要建立不同的 layout 時,上述方法的 1 跟 4 代表的就會不太一樣。
當 routes/dashboard._index.tsx 存在時,routes/dashboard.tsx 就會被當作 layout 的檔案,只要在其中使用 <Outlet/> 這個 component 就可以了。