Pages directory

The pages directory contains your Application Views and Routes. Nuxt reads all the .vue files inside this directory and automatically creates the router configuration for you.


Pages directory - 图1

You can also create routes with .js files and .ts files

Every Page component is a Vue component but Nuxt adds special attributes and functions to make the development of your universal application as easy as possible.

pages

  1. <template>
  2. <h1 class="red">Hello {{ name }}!</h1>
  3. </template>
  4. <script>
  5. export default {
  6. // page properties go here
  7. }
  8. </script>
  9. <style>
  10. .red {
  11. color: red;
  12. }
  13. </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 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

  1. <template>
  2. <h1>{{ slug }}</h1>
  3. </template>
  4. <script>
  5. export default {
  6. async asyncData({ params }) {
  7. const slug = params.slug // When calling /abc the slug will be "abc"
  8. return { slug }
  9. }
  10. }
  11. </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

  1. <template>
  2. <h1>{{ book }} / {{ slug }}</h1>
  3. </template>
  4. <script>
  5. export default {
  6. async asyncData({ params }) {
  7. const book = params.book
  8. const slug = params.slug
  9. return { book, slug }
  10. }
  11. }
  12. </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

  1. export default {
  2. asyncData(context) {
  3. return { name: 'World' }
  4. }
  5. }

Pages directory - 图2

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.

  1. <script>
  2. export default {
  3. data() {
  4. return {
  5. posts: []
  6. }
  7. },
  8. async fetch() {
  9. this.posts = await fetch('https://api.nuxtjs.dev/posts').then(res =>
  10. res.json()
  11. )
  12. }
  13. }
  14. </script>

Pages directory - 图3

See more on how fetch works in our Data Fetching chapter

head

Set specific

tags for the current page. Nuxt uses vue-meta to update the document head and meta attributes of your application.

pages/index.vue

  1. export default {
  2. head() {
  3. // Set Meta Tags for this Page
  4. }
  5. }

Pages directory - 图4

See more in our Meta Tags and SEO chapter

layout

Specify a layout defined in the layouts directory.

pages/index.vue

  1. export default {
  2. layout: 'blog'
  3. }

Pages directory - 图5

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

  1. export default {
  2. loading: false
  3. }

Pages directory - 图6

Only applies if loading is also set in nuxt.config.js.

Pages directory - 图7

See more in our Loading chapter.

transition

Defines a specific transition for the page.

pages/index.vue

  1. export default {
  2. transition: 'fade'
  3. }

Pages directory - 图8

See more on transitions in our Transitions chapter

scrollToTop

The scrollToTop property lets you tell Nuxt to scroll to the top before rendering the page. By default, Nuxt scrolls to the top when you go to another page, but with child routes, Nuxt keeps the scroll position. If you want to tell Nuxt to scroll to the top when rendering your child route, set scrollToTop to true

pages/index.vue

  1. export default {
  2. scrollToTop: true
  3. }

Conversely, you can manually set scrollToTop to false on parent routes as well.

If you want to overwrite the default scroll behavior of Nuxt, 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

  1. export default {
  2. middleware: 'auth'
  3. }

Pages directory - 图9

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(context), validate, layout, …) will be called. Watching is disabled by default to improve performance.

pages/index.vue

  1. export default {
  2. watchQuery: ['page']
  3. }

Pages directory - 图10

Warning: The new fetch hook introduced in 2.12 is not affected by watchQuery. For more information see listening to query string changes.

pages/index.vue

  1. export default {
  2. watchQuery: true
  3. }

You can also use the function watchQuery(newQuery, oldQuery) to have more refined watchers.

pages/index.vue

  1. export default {
  2. watchQuery(newQuery, oldQuery) {
  3. // Only execute component methods if the old query string contained `bar`
  4. // and the new query string contains `foo`
  5. return newQuery.foo && oldQuery.bar
  6. }
  7. }

Pages directory - 图11

See more on the watch query property in our Data Fetching chapter

key

Same as the key property that can be used on Vue components in templates as a hint for the virtual DOM, this property allows the key value to be defined from the page itself (rather than the parent component).

By default in Nuxt, this value will be the $route.path, meaning that navigating to a different route will ensure a clean page component is created. Logically equivalent to:

  1. <router-view :key="$route.path" />

The property can be a String or a Function which takes the route as the first argument.

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.

Pages directory - 图12

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

  1. export default {
  2. dir: {
  3. // Rename `pages` directory to `routes`
  4. pages: 'routes'
  5. }
  6. }

Pages directory - 图13

Checkout the dir option to learn more about it.