The pages
directory contains your application views and routes. Nuxt.js reads all the .vue
files inside this directory and automatically creates the router configuration for you.
You can also create routes with .js files and .ts files
Every Page component is a Vue component but Nuxt.js adds special attributes and functions to make the development of your universal application as easy as possible.
<template>
<h1 class="red">Hello {{ name }}!</h1>
</template>
<script>
export default {
// page properties go here
}
</script>
<style>
.red {
color: red;
}
</style>
Dynamic Pages
Dynamic pages can be created when you don’t know the name of the page due to it coming from an API or you don’t want to have to create the same page over and over again. To create a dynamic page you need to add an underscore before the .vue file name or before the the name of the directory, if you want the directory to be dynamic. You can name the file or directory anything you want but you must prefix it with an underscore.
If you’ve defined a file named _slug.vue
in your pages folder, you can access the value using the context with params.slug
pages/_slug.vue
<template>
<h1>{{ this.slug }}</h1>
</template>
<script>
export default {
async asyncData({ params }) {
const slug = params.slug // When calling /abc the slug will be "abc"
return { slug }
}
}
</script>
If you’ve defined a file named _slug.vue inside a folder called _book you can access the value using the context with params.slug and params.book
pages/_book/_slug.vue
<template>
<h1>{{ this.book }} / {{ this.slug }}</h1>
</template>
<script>
export default {
async asyncData({ params }) {
const book = params.book
const slug = params.slug
return { book, slug }
}
}
</script>
Properties
asyncData
asyncData is called every time before loading the component. It can be asynchronous and receives the context as an argument. The returned object will be merged with your data object.
pages/index.vue
export default {
asyncData(context) {
return { name: 'World' }
}
}
See more on how asyncData works in our Data Fetching chapter
fetch
Every time you need to get asynchronous data you can use fetch. Fetch is called on server-side when rendering the route, and on client-side when navigating.
<script>
export default {
data() {
return {
posts: []
}
},
async fetch() {
this.posts = await fetch('https://api.nuxtjs.dev/posts').then(res =>
res.json()
)
}
}
</script>
See more on how fetch works in our Data Fetching chapter
head
Set specific
tags for the current page. Nuxt.js uses vue-meta
to update the document head and meta attributes of your application.
pages/index.vue
export default {
head() {
// Set Meta Tags for this Page
}
}
See more in our Meta Tags and SEO chapter
layout
Specify a layout defined in the layouts directory.
pages/index.vue
export default {
layout: 'blog'
}
See more on layouts in our Views chapter.
loading
If set to false, prevents a page from automatically calling this.$nuxt.$loading.finish()
as you enter it and this.$nuxt.$loading.start()
as you leave it, allowing you to manually control the behavior, as this example shows.
pages/index.vue
export default {
loading: false
}
Only applies if loading is also set in nuxt.config.js.
See more in our Loading chapter.
transition
Defines a specific transition for the page.
pages/index.vue
export default {
transition: 'fade'
}
See more on transitions in our Transitions chapter
scrollToTop
The scrollToTop
property lets you tell Nuxt.js to scroll to the top before rendering the page. By default, Nuxt.js scrolls to the top when you go to another page, but with child routes, Nuxt.js keeps the scroll position. If you want to tell Nuxt.js to scroll to the top when rendering your child route, set scrollToTop
to true
pages/index.vue
export default {
scrollToTop: true
}
Conversely, you can manually set scrollToTop
to false
on parent routes as well.
If you want to overwrite the default scroll behavior of Nuxt.js, take a look at the scrollBehavior option.
middleware
Defines middleware for this page. The middleware will be called before rendering the page.
pages/index.vue
export default {
middleware: 'auth'
}
See more on middleware in our Middleware chapter
The watchQuery Property
Use the watchQuery
key to set up a watcher for query strings. If the defined strings change, all component methods (asyncData, fetch, validate, layout, …) will be called. Watching is disabled by default to improve performance.
pages/index.vue
export default {
watchQuery: ['page']
}
If you want to set up a watcher for all query strings, set watchQuery
to true
.
pages/index.vue
export default {
watchQuery: true
}
You can also use the function watchQuery(newQuery, oldQuery)
to have more refined watchers.
pages/index.vue
export default {
watchQuery(newQuery, oldQuery) {
// Only execute component methods if the old query string contained `bar`
// and the new query string contains `foo`
return newQuery.foo && oldQuery.bar
}
}
See more on the watch query property in our Data Fetching chapter
Ignoring pages
If you want to ignore pages so that they are not included in the generated router.js
file then you can ignore them by prefixing them with a -
.
For example, pages/-about.vue
will be ignored.
Checkout the ignore option to learn more about it.
Configuration
You can rename the pages/
directory to something different by setting dir.pages
option:
nuxt.config.js
export default {
dir: {
// Rename `pages` directory to `routes`
pages: 'routes'
}
}
Checkout the dir option to learn more about it.