Nuxt/examples/custom-routes/README.md

186 lines
3.2 KiB
Markdown
Raw Normal View History

# Defining custom routes with Nuxt.js
2016-12-20 09:38:16 +00:00
> Nuxt.js is based on `vue-router` and let you to defined custom routes easily :rocket:
2016-12-13 10:04:55 +00:00
## Concept
2016-12-20 09:38:16 +00:00
Nuxt.js generates automatically the `vue-router` configuration according to your file tree of `.vue` files inside the `pages/` directory.
2016-12-13 10:04:55 +00:00
## Basic routes
This file tree:
```bash
2016-12-20 09:38:16 +00:00
pages/
--| team/
-----| index.vue
-----| about.vue
--| index.vue
2016-12-13 10:04:55 +00:00
```
will automatically generate:
```js
router: {
routes: [
{
name: 'index',
path: '/',
2016-12-20 09:38:16 +00:00
component: 'pages/index.vue'
2016-12-13 10:04:55 +00:00
},
{
name: 'team',
path: '/team',
2016-12-20 09:38:16 +00:00
component: 'pages/team/index.vue'
2016-12-13 10:04:55 +00:00
},
{
name: 'team-about',
path: '/team/about',
2016-12-20 09:38:16 +00:00
component: 'pages/team/about.vue'
2016-12-13 10:04:55 +00:00
}
]
}
```
## Dynamic routes
2016-12-20 09:38:16 +00:00
To define a dynamic route with a param, you need to define a `.vue` file **prefixed by an underscore**.
2016-12-13 10:04:55 +00:00
This file tree:
```bash
2016-12-20 09:38:16 +00:00
pages/
--| users/
-----| _id.vue
-----| index.vue
2016-12-13 10:04:55 +00:00
```
will automatically generate:
```js
router: {
routes: [
{
2016-12-20 09:38:16 +00:00
name: 'users',
path: '/users',
component: 'pages/users/index.vue'
2016-12-13 10:04:55 +00:00
},
{
2016-12-20 09:38:16 +00:00
name: 'users-id',
path: '/users/:id',
component: 'pages/users/_id.vue'
2016-12-13 10:04:55 +00:00
}
]
}
```
2016-12-20 09:38:16 +00:00
### Additional feature: validate (optional)
2016-12-13 10:04:55 +00:00
2016-12-20 09:38:16 +00:00
Nuxt.js lets you define a validator function inside your dynamic route component (In this example: `pages/users/_id.vue`).
2016-12-13 10:06:11 +00:00
2016-12-20 09:38:16 +00:00
If the validate method does not return `true`, Nuxt.js will automatically load the 404 error page.
```js
2016-12-13 10:04:55 +00:00
<script>
export default {
validate ({ params }) {
2016-12-20 09:38:16 +00:00
return /^\d+$/.test(params.id)
2016-11-10 16:16:37 +00:00
}
}
2016-12-13 10:04:55 +00:00
</script>
```
2016-12-13 10:04:55 +00:00
## Nested Routes (children)
2016-12-20 09:38:16 +00:00
To define a nested route, you need to create a `.vue` file with the **same name as the directory** which contain your children views.
> Don't forget to put `<nuxt-child></nuxt-child>` inside your parent `.vue` file.
2016-12-13 10:04:55 +00:00
This file tree:
2016-12-13 10:04:55 +00:00
```bash
2016-12-20 09:38:16 +00:00
pages/
--| users/
-----| _id.vue
--| users.vue
2016-12-13 10:04:55 +00:00
```
will automatically generate:
```js
2016-12-13 10:04:55 +00:00
router: {
routes: [
{
2016-12-13 10:08:23 +00:00
path: '/users',
2016-12-20 09:38:16 +00:00
component: 'pages/users.vue',
2016-12-13 10:08:23 +00:00
children: [
{
path: ':id',
2016-12-20 09:38:16 +00:00
component: 'pages/users/_id.vue',
2016-12-13 10:08:23 +00:00
name: 'users-id'
}
]
}
2016-12-13 10:04:55 +00:00
]
}
```
## Dynamic Nested Routes
This file tree:
```bash
2016-12-20 09:38:16 +00:00
pages/
--| posts/
-----| _slug/
--------| _name.vue
--------| comments.vue
-----| _slug.vue
-----| index.vue
--| posts.vue
2016-12-13 10:04:55 +00:00
```
will automatically generate:
```js
router: {
routes: [
{
2016-12-13 10:08:23 +00:00
path: '/posts',
2016-12-20 09:38:16 +00:00
component: 'pages/posts.vue',
2016-12-13 10:08:23 +00:00
children: [
{
2016-12-20 09:38:16 +00:00
         path '',
component: 'pages/posts/index.vue',
2016-12-13 10:08:23 +00:00
name: 'posts'
},
{
path: ':slug',
2016-12-20 09:38:16 +00:00
component: 'pages/posts/_slug.vue',
2016-12-13 10:08:23 +00:00
children: [
{
path: 'comments',
2016-12-20 09:38:16 +00:00
component: 'pages/posts/_slug/comments.vue',
2016-12-13 10:08:23 +00:00
name: 'posts-slug-comments'
},
{
path: ':name',
2016-12-20 09:38:16 +00:00
component: 'pages/posts/_slug/_name.vue',
2016-12-13 10:08:23 +00:00
name: 'posts-slug-name'
}
]
}
]
}
2016-12-13 10:04:55 +00:00
]
}
```
## Demo
```bash
npm install
npm start
```
Go to [http://localhost:3000](http://localhost:3000) and navigate through the pages.