介紹
學習Nuxt3順手把它使用上的重點給記錄下來
Router設定
1.創建 folder/pages
2.修改 app.vue
1 | <template> |
3.如果需要透過Router使用 <NuxtLink to="/"></NuxtLink>
4.如果要設定動態Router使用方括號, ex: /user-[group]/[id]
, 讀取方框號值使用 {{$route.params.key}}
5.嵌套路由 需創建同名folder與file 創建 folder/parent
與 parent.vue
, /parent
底下可以有預設頁面index.vue
也可以有其他child.vue
,而外層的parent.vue
需要使用<NuxtChild></NuxtChild>
當子組件出口, Router就能使用 /parent/child
指到該路由
Layouts設定
1.創建 folder/layouts
2.在/layouts
底下創建default.vue
3.新增default.vue內容
1 | <template> |
4.修改app.vue
1 | <template> |
補充
也可以使用以下方式載入對應的layout<NuxtLayout name="layout Name">
或者是透過Script
1 | <script> |
Components 設定
1.創建 folder/Components
2.創建component file component.vue
在template中可以直接呼叫, 但要注意大小寫名稱依照創建的file為主
補充
如果是在透過路徑下的方式呼叫component會是依照 folder name組合來呼叫, ex:/components/base/btn/button.vue
, 那呼叫的名稱就會是BaseBtnButton
以此類推