npm

Check out our latest tech talk, "JavaScript Supply Chain Security" presented by VP of Security, Adam Baldwin.Watch it here »

@nuxtjs/router

1.3.2 • Public • Published

Nuxt Router Module

npm version npm downloads Circle CI Codecov Dependencies Standard JS

Nuxt module to use router.js instead of pages/ directory.

📖 Release Notes

Features

Use your own router.js to handle your routes into your Nuxt.js application.

Setup

  1. Add @nuxtjs/router dependency with yarn or npm into your project
  2. Add @nuxtjs/router to modules section of nuxt.config.js:
  3. Configure it:
{
  modules: [
    '@nuxtjs/router'
  ]
}

or

{
  modules: [
    ['@nuxtjs/router', { keepDefaultRouter: true }]
  ]
}

If you are using SPA mode, add an index / route to generate section of nuxt.config.js:

{
  generate: {
    routes: [
      '/'
    ]
  }
}

Options

path

  • Default: srcDir

Location of you route file.

fileName

  • Default: router.js

Name of you route file.

keepDefaultRouter

  • Default: false

Can access the default router.

Usage

This module disable the pages/ directory into Nuxt and will use a router.js file at your srcDir directory:

components/
  my-page.vue
router.js

router.js need to export a createRouter method like this:

import Vue from 'vue'
import Router from 'vue-router'
 
import MyPage from '~/components/my-page'
 
Vue.use(Router)
 
export function createRouter() {
  return new Router({
    mode: 'history',
    routes: [
      {
        path: '/',
        component: MyPage
      }
    ]
  })
}

Accessing default router

If you use the module with { keepDefaultRouter: true }, you can access the default router:

export function createRouter(ssrContext, createDefaultRouter) {
  const defaultRouter = createDefaultRouter(ssrContext)
  return new Router({
    ...defaultRouter.options,
    routes: fixRoutes(defaultRouter.options.routes)
  })
}
 
function fixRoutes(defaultRoutes) {
  // default routes that come from `pages/`
  return defaultRoutes.filter(...).map(...)
}

License

MIT License

Copyright (c) Nuxt Community

Keywords

none

install

npm i @nuxtjs/router

Downloadsweekly downloads

5,471

version

1.3.2

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
Report a vulnerability