Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@nuxtjs/router

Package Overview
Dependencies
Maintainers
6
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nuxtjs/router

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

  • 1.5.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
20K
increased by4.18%
Maintainers
6
Weekly downloads
 
Created
Source

@nuxtjs/router

npm version npm downloads Circle CI Codecov License

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 to your project
yarn add @nuxtjs/router # or npm install @nuxtjs/router
  1. Add @nuxtjs/router to the buildModules section of nuxt.config.js

:warning: If you are using Nuxt < 2.9.0, use modules instead.

{
  buildModules: [
    // Simple usage
    '@nuxtjs/router',

    // With options
    ['@nuxtjs/router', { /* module options */ }]
  ]
}

Using top level options

{
  buildModules: [
    '@nuxtjs/router'
  ],
  routerModule: {
    /* module options */
  }
}

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, routerOptions) {
  const options = routerOptions ? routerOptions : createDefaultRouter(ssrContext).options

  return new Router({
    ...options,
    routes: fixRoutes(options.routes)
  })
}

function fixRoutes(defaultRoutes) {
  // default routes that come from `pages/`
  return defaultRoutes.filter(...).map(...)
}

License

MIT License

Copyright (c) Nuxt Community

FAQs

Package last updated on 21 Aug 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc