Routing

Most websites have more than just one page. For example a home page, about page, contact page etc. In order to show these pages we need a Router.


Automatic Routes

Most websites will have more than one page (i.e. a home page, about page, contact page etc.). In order to show these pages, we need a Router. That’s where vue-router comes in. When working with the Vue application, you have to set up a configuration file (i.e. router.js) and add all your routes manually to it. Nuxt automatically generates the vue-router configuration for you, based on your provided Vue files inside the pages directory. That means you never have to write a router config again! Nuxt also gives you automatic code-splitting for all your routes.

In other words, all you have to do to have routing in your application is to create .vue files in the pages folder.

Routing - 图1

Learn more about Routing

Navigation

To navigate between pages of your app, you should use the NuxtLink component. This component is included with Nuxt and therefore you don’t have to import it as you do with other components. It is similar to the HTML <a> tag, except that instead of using a href="/about" we use to="/about". If you have used vue-router before, you can think of the <NuxtLink> as a replacement for <RouterLink>

A simple link to the index.vue page in your pages folder:

pages/index.vue

  1. <template>
  2. <NuxtLink to="/">Home page</NuxtLink>
  3. </template>

For all links to pages within your site, use <NuxtLink>. If you have links to other websites you should use the <a> tag. See below for an example:

pages/index.vue

  1. <template>
  2. <main>
  3. <h1>Home page</h1>
  4. <NuxtLink to="/about">
  5. About (internal link that belongs to the Nuxt App)
  6. </NuxtLink>
  7. <a href="https://nuxtjs.org">External Link to another page</a>
  8. </main>
  9. </template>

Routing - 图2

Learn more about the NuxtLink component.