Page Routes
Master creating and managing page routes in ShipAhead using Nuxt’s file-based routing system for organized navigation.
Tools Used
- Nuxt: Provides file-based routing to automatically generate page routes from the
app/pages/directory.
Setup & Configuration
Nuxt automatically creates routes based on files in the app/pages/ directory. No manual configuration is needed unless customizing routes with definePageMeta.
Add a Page:
- Create a
.vuefile inapp/pages/(e.g.,app/pages/about.vuefor the/aboutroute). - Example:
app/pages/about.vue
<template> <div>About ShipAhead</div> </template>
- Create a
Dynamic Routes:
- Use square brackets for dynamic parameters (e.g.,
app/pages/blogs/[slug].vuefor/blogs/my-post). - Example:
app/pages/blogs/[slug].vue
<template> <div>Blog: {{ useRoute().params.slug }}</div> </template>
- Use square brackets for dynamic parameters (e.g.,
Nested Routes:
- Create a folder with an
index.vuefile (e.g.,app/pages/dashboard/index.vuefor/dashboard). - Add child routes in the same folder (e.g.,
app/pages/dashboard/settings.vuefor/dashboard/settings).
- Create a folder with an
Usage
Basic Routing: Add
.vuefiles toapp/pages/to create routes automatically (e.g.,app/pages/contact.vuecreates/contact).Customizing Pages: Use
definePageMetato set a layout or middleware (e.g., for admin pages requiring authentication):app/pages/admin/index.vuedefinePageMeta({ layout: 'admin', // Uses layouts/admin.vue requireAuth: true, // Applies authentication middleware });layout: 'admin': Applies the admin layout fromlayouts/admin.vue.requireAuth: true: Restricts access to authenticated users, defined in ShipAhead’s middleware.
Dynamic Parameters: Access dynamic route parameters (e.g.,
slug) usinguseRoute()in your components.Verify Routes: Test routes by running
npm run devand navigating to URLs (e.g.,http://localhost:3000/about).Learn More: For advanced routing, visit Nuxt Pages Documentation.