File system routing

Nuxt automatically generates the vue-router configuration based on your file tree of Vue files inside the pages directory. When you create a .vue file in your pages directory you will have basic routing working with no extra configuration needed.


Sometimes you might need to create dynamic routes or nested routes or you might need to further configure the router property. This chapter will go through everything you need to know in order to get the best out of your router.

File System Routing - 图1

Nuxt gives you automatic code splitting for your routes, no configuration is needed

File System Routing - 图2

Use the NuxtLink component to navigate between pages

  1. <template>
  2. <NuxtLink to="/">Home page</NuxtLink>
  3. </template>

Basic Routes

This file tree:

  1. pages/
  2. --| user/
  3. -----| index.vue
  4. -----| one.vue
  5. --| index.vue

will automatically generate:

  1. router: {
  2. routes: [
  3. {
  4. name: 'index',
  5. path: '/',
  6. component: 'pages/index.vue'
  7. },
  8. {
  9. name: 'user',
  10. path: '/user',
  11. component: 'pages/user/index.vue'
  12. },
  13. {
  14. name: 'user-one',
  15. path: '/user/one',
  16. component: 'pages/user/one.vue'
  17. }
  18. ]
  19. }

Dynamic Routes

Sometimes it is not possible to know the name of the route such as when we make a call to an API to get a list of users or blog posts. We call these dynamic routes. To create a dynamic route you need to add an underscore (_) before the .vue file name or before the name of the directory. You can name the file or directory anything you want but you must prefix it with an underscore.

This file tree:

  1. pages/
  2. --| _slug/
  3. -----| comments.vue
  4. -----| index.vue
  5. --| users/
  6. -----| _id.vue
  7. --| index.vue

will automatically generate:

  1. router: {
  2. routes: [
  3. {
  4. name: 'index',
  5. path: '/',
  6. component: 'pages/index.vue'
  7. },
  8. {
  9. name: 'users-id',
  10. path: '/users/:id?',
  11. component: 'pages/users/_id.vue'
  12. },
  13. {
  14. name: 'slug',
  15. path: '/:slug',
  16. component: 'pages/_slug/index.vue'
  17. },
  18. {
  19. name: 'slug-comments',
  20. path: '/:slug/comments',
  21. component: 'pages/_slug/comments.vue'
  22. }
  23. ]
  24. }

File System Routing - 图3

As you can see the route named users-id has the path :id? which makes it optional, if you want to make it required, create an index.vue file in the users/_id directory instead.

File System Routing - 图4

As of Nuxt >= v2.13 there is a crawler installed that will now crawl your link tags and generate your dynamic routes based on those links. However if you have pages that are not linked to such as a secret page, then you will need to manually generate those dynamic routes.

File System Routing - 图5

Generate dynamic routes for static sites

Locally Accessing Route Params

You can access the current route parameters within your local page or component by referencing this.$route.params.{parameterName}. For example, if you had a dynamic users page (users/_id.vue) and wanted to access the id parameter to load the user or process information, you could access the variable like this: this.$route.params.id.

Nested Routes

Nuxt lets you create nested routes by using the children routes of vue-router. To define the parent component of a nested route, you need to create a Vue file with the same name as the directory which contains your children views.

File System Routing - 图6

Don’t forget to include the NuxtChild component inside the parent component (.vue file).

This file tree:

  1. pages/
  2. --| users/
  3. -----| _id.vue
  4. -----| index.vue
  5. --| users.vue

will automatically generate:

  1. router: {
  2. routes: [
  3. {
  4. path: '/users',
  5. component: 'pages/users.vue',
  6. children: [
  7. {
  8. path: '',
  9. component: 'pages/users/index.vue',
  10. name: 'users'
  11. },
  12. {
  13. path: ':id',
  14. component: 'pages/users/_id.vue',
  15. name: 'users-id'
  16. }
  17. ]
  18. }
  19. ]
  20. }

Dynamic Nested Routes

This is not a common scenario, but it is possible with Nuxt to have dynamic children inside dynamic parents.

This file tree:

  1. pages/
  2. --| _category/
  3. -----| _subCategory/
  4. --------| _id.vue
  5. --------| index.vue
  6. -----| _subCategory.vue
  7. -----| index.vue
  8. --| _category.vue
  9. --| index.vue

will automatically generate:

  1. router: {
  2. routes: [
  3. {
  4. path: '/',
  5. component: 'pages/index.vue',
  6. name: 'index'
  7. },
  8. {
  9. path: '/:category',
  10. component: 'pages/_category.vue',
  11. children: [
  12. {
  13. path: '',
  14. component: 'pages/_category/index.vue',
  15. name: 'category'
  16. },
  17. {
  18. path: ':subCategory',
  19. component: 'pages/_category/_subCategory.vue',
  20. children: [
  21. {
  22. path: '',
  23. component: 'pages/_category/_subCategory/index.vue',
  24. name: 'category-subCategory'
  25. },
  26. {
  27. path: ':id',
  28. component: 'pages/_category/_subCategory/_id.vue',
  29. name: 'category-subCategory-id'
  30. }
  31. ]
  32. }
  33. ]
  34. }
  35. ]
  36. }

Unknown Dynamic Nested Routes

If you do not know the depth of your URL structure, you can use _.vue to dynamically match nested paths. This will handle requests that do not match a more specific route.

This file tree:

  1. pages/
  2. --| people/
  3. -----| _id.vue
  4. -----| index.vue
  5. --| _.vue
  6. --| index.vue

Will handle requests like this:

  1. / -> index.vue
  2. /people -> people/index.vue
  3. /people/123 -> people/_id.vue
  4. /about -> _.vue
  5. /about/careers -> _.vue
  6. /about/careers/chicago -> _.vue

File System Routing - 图7

Handling 404 pages is now up to the logic of the _.vue page.

Extending the router

There are multiple ways to extend the routing with Nuxt:

The router Property

The router property lets you customize the Nuxt router (vue-router).

nuxt.config.js

  1. export default {
  2. router: {
  3. // customize the Nuxt router
  4. }
  5. }

Base:

The base URL of the app. For example, if the entire single page application is served under /app/, then base should use the value '/app/'.

File System Routing - 图8

Router Base Property

extendRoutes

You may want to extend the routes created by Nuxt. You can do so via the extendRoutes option.

Example of adding a custom route:

nuxt.config.js

  1. export default {
  2. router: {
  3. extendRoutes(routes, resolve) {
  4. routes.push({
  5. name: 'custom',
  6. path: '*',
  7. component: resolve(__dirname, 'pages/404.vue')
  8. })
  9. }
  10. }
  11. }

If you want to sort your routes, you can use the sortRoutes(routes) function from @nuxt/utils:

nuxt.config.js

  1. import { sortRoutes } from '@nuxt/utils'
  2. export default {
  3. router: {
  4. extendRoutes(routes, resolve) {
  5. // Add some routes here ...
  6. // and then sort them
  7. sortRoutes(routes)
  8. }
  9. }
  10. }

File System Routing - 图9

The schema of the route should respect the vue-router schema.

File System Routing - 图10

When adding routes that use Named Views, don’t forget to add the corresponding chunkNames of named components.

nuxt.config.js

  1. export default {
  2. router: {
  3. extendRoutes(routes, resolve) {
  4. routes.push({
  5. path: '/users/:id',
  6. components: {
  7. default: resolve(__dirname, 'pages/users'), // or routes[index].component
  8. modal: resolve(__dirname, 'components/modal.vue')
  9. },
  10. chunkNames: {
  11. modal: 'components/modal'
  12. }
  13. })
  14. }
  15. }
  16. }

File System Routing - 图11

extendRoutes Property

fallback

Controls whether the router should fallback to hash mode when the browser does not support history.pushState but mode is set to history.

File System Routing - 图12

fallback Property

mode

Configure the router mode, it is not recommended to change it due to server-side rendering.

File System Routing - 图13

mode Property

parseQuery / stringifyQuery

Provide custom query string parse / stringify functions.

File System Routing - 图14

parseQuery / stringifyQuery Property

routeNameSplitter

You may want to change the separator between route names that Nuxt uses. You can do so via the routeNameSplitter option in your configuration file. Imagine we have the page file pages/posts/_id.vue. Nuxt will generate the route name programmatically, in this case posts-id. Changing the routeNameSplitter config to / the name will therefore change to posts/id.

nuxt.config.js

  1. export default {
  2. router: {
  3. routeNameSplitter: '/'
  4. }
  5. }

scrollBehavior

The scrollBehavior option lets you define a custom behavior for the scroll position between the routes. This method is called every time a page is rendered.

File System Routing - 图15

To learn more about it, see vue-router scrollBehavior documentation.

Available since:v2.9.0:

In Nuxt you can use a file to overwrite the router scrollBehavior. This file should be placed in a folder called app.

~/app/router.scrollBehavior.js.

Example of forcing the scroll position to the top for every route:

app/router.scrollBehavior.js

  1. export default function (to, from, savedPosition) {
  2. return { x: 0, y: 0 }
  3. }

File System Routing - 图16

Nuxt default router.scrollBehavior.js file.

File System Routing - 图17

scrollBehavior Property

trailingSlash

Available since: v2.10

If this option is set to true, trailing slashes will be appended to every route. If set to false, they’ll be removed.

nuxt.config.js

  1. export default {
  2. router: {
  3. trailingSlash: true
  4. }
  5. }

File System Routing - 图18

This option should not be set without preparation and has to be tested thoroughly. When setting router.trailingSlash to something else other than undefined(which is the default value), the opposite route will stop working. Thus 301 redirects should be in place and your internal linking has to be adapted correctly. If you set trailingSlash to true, then only example.com/abc/ will work but not example.com/abc. On false, it’s vice-versa.

File System Routing - 图19

trailingSlash Property