🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

@ax2/router

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ax2/router

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

latest
Source
npmnpm
Version
2.0.0
Version published
Maintainers
1
Created
Source

Nuxt Router Module

npm (scoped with tag) npm CircleCI Codecov

js-standard-style

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

  • Install @nuxtjs/router dependency with yarn or npm into your project
  • Add @nuxtjs/router to modules section of nuxt.config.js:
{
  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: [
      '/'
    ]
  }
}

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:

import { createRouter as createDefaultRouter } from './defaultRouter'

export function createRouter(ssrContext) {
  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) Sebastien Chopin seb@chopin.io

FAQs

Package last updated on 16 Nov 2018

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