Nuxt enables automatic routing. All you need to do is -
- Create the Nuxt app
- Create Vue pages under
pagesroot directory
All pages will automatically be routes in the app - no need for a distinct Vue router definition.
Automatic Routes in Nuxt
Consider the below pages in an example app https://my-app.com-
- Create
/pages/index.vuefor your home page => maps to => https://my-app.com/ - Create
/pages/contact.vuefor your contact page => maps to => (https://my-app.com/contact - Create
/pages/customers.vuefor to display customers => maps to => https://my-app.com/customers
The directory structure in pages..
pages/
--| index.vue
--| contact.vue
--| customers.vue
.. is equivalent to the below router file -
router: {
routes: [
{
path: '/',
component: 'pages/index.vue',
},
{
path: '/contact',
component: 'pages/contact.vue',
},
{
path: '/customers',
component: 'pages/customers.vue',
},
]
}
You can also have dynamic routes -
pages/
--| _id.vue
.. which is equivalent to => https://my-app.com/a1iDThis
.. or nested routes..
pages/
--| customers/
-----| _id.vue
-----| index.vue
--| contact.vue
--| index.vue
.. which will generate => -
- https://my-app.com/customers/c123
- https://my-app.com/customers/
- https://my-app.com/contact/
- https://my-app.com/
Using Route Middleware
Use middleware to perform dark magic on routes. You can protect routes and make them accessible only for authenticated users for example.
Create middleware directory in Nuxt root folder, and create a new file called auth.js therein.
export default function ({ store, redirect }) {
if (process.client && !store.getters["isLoggedIn"]) return redirect("/login");
}
The above code is doing two things -
- check whether current execution context is
client(we don’t want this check to happen on server - our cookie/session variables exist in client browser ) - if current user is not logged in (provided by
isLoggedIn), redirect to/loginpage
Tell Nuxt about this middleware - edit nuxt.config.js -
export default {
// ..
middleware: ["auth"],
// ..
};
To use this middleware, just include the below code block in your page -
<script>
export default {
middleware: "auth",
};
</script>
Now, Nuxt will redirect user to /login page if not already authenticated (i.e., isLoggedIn is false).
Override Default Behaviour: Bring your own router
You can always assume more control in routing if you are still getting adjusted to the idea of “automatic routing”, or simply have too many things going on during routing.
Use router-module in your Nuxt project.
Install
npm i --save @nuxtjs/routerAdd to build modules in
nuxt.config.jsexport default { buildModules: ["@nuxtjs/router"], };Create
router.jsin your Nuxt project and include the “normal” router codeimport Vue from "vue"; import Router from "vue-router"; import Index from "~/components/index"; import Contact from "~/components/contact"; Vue.use(Router); export function createRouter() { return new Router({ mode: "history", routes: [ { path: "/", component: Index }, { path: "/contact", component: Contact }, ], }); }