nuxt-community / router-extras-module

Extra Add-ons For Nuxt 2 Router
https://codesandbox.io/s/github/nuxt-community/router-extras-module
MIT License
279 stars 12 forks source link
nuxt-module nuxtjs vue-router

@nuxtjs/router-extras

npm version npm downloads Circle CI Codecov License

Extra add-ons for Nuxt 2 router

Demo: https://codesandbox.io/s/github/nuxt-community/router-extras-module

📖 Release Notes

Nuxt 3

Looking for a Nuxt 3 alternative? Use the core composable definePageMeta.

Example:

<script setup>
definePageMeta({
  path: '/posts',
  alias: ['/articles', '/blog']
})
</script>

Features

Setup

  1. Add @nuxtjs/router-extras dependency to your project
yarn add --dev @nuxtjs/router-extras # or npm install --save-dev @nuxtjs/router-extras
  1. Add @nuxtjs/router-extras 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-extras',

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

Using top level options

{
  buildModules: [
    '@nuxtjs/router-extras'
  ],
  routerExtras: {
    /* module options */
  }
}

Options

routerNativeAlias

Simple aliases will be added as router alias, see vue-router

Usage

Define custom paths for a page

Simply add a block inside Vue file and define a path in JavaScript or Yaml

JavaScript ```xml { path: '/posts' } ```
Yaml ```xml path: /posts ```

Define multiple aliases for single page

If you want more paths for a single page, define them with aliases

JavaScript ```xml { path: '/posts', alias: [ '/articles', '/blog' ] } ```
Yaml ```xml path: /posts alias: - /articles - /blog ```

Aliases can have their own props

JavaScript ```xml { path: '/posts', alias: [ '/articles', { path: '/blog', props: { section: 'top-posts' } } ] } ```
Yaml ```xml path: /posts alias: - /articles - path: /blog props: section: top-posts ```

Define multiple params regardless of pages directory structure

JavaScript ```xml { path: '/post/:id/:title?' } ```
Yaml ```xml path: /post/:id/:title? ```

Define named views for the page

JavaScript ```xml { namedViews: { currentView: 'main', views: { side: '~/components/side.vue' }, chunkNames: { side: 'components/side' } } } ```
Yaml ```xml namedViews: currentView: "main" views: side: "~/components/side.vue" chunkNames: side: "~/components/side.vue" ```

Valid Extras

Extras Support Description
path JS & YAML Change page URL
alias JS & YAML Add single or multiple aliases to page, Module supports two types of aliases
- Simple Alias: These aliases are defined as simple strings. If routerNativeAlias is true, simple aliases will be added as router alias, see vue-router docs
- Clone Alias: These aliases are in form of object and they can have their own extras. These aliases will be added as an individual route. They can have their own props and they can have different number of url params
meta JS & YAML Add Meta information to the page, meta can be used by middlewares
name JS & YAML Define custom name for route
props JS & YAML Pass predefined props to page
beforeEnter JS Define beforeEnter guard for this route, see: Global Before Guards
caseSensitive JS & YAML Use case sensitive route match (default: false)
redirect JS & YAML Redirect current page to new location
namedViews JS & YAML Add named view to the path, see Named Views Support

Named views support

There is support for named views in nuxt, but it requires the user to write a lot of boilerplate code in the config. The namedViews property in the <router> block allows for a more streamlined configuration

Named views key is a bit different from all other settings. It expects an object with following properties:

For usage example see example/pages/namedParent.vue and example/pages/namedParent/namedChild.vue.

Syntax Highlighting

Visual Studio Code

Install Vetur extension and define custom block

"vetur.grammar.customBlocks": {
    "docs": "md",
    "i18n": "json",
    "router": "js"
}

PhpStorm/WebStorm

Development

License

MIT License

Copyright (c) Nuxt Community