Nuxt.js comes with a few important components included out of the box, which will be helpful when building your application. The components are globally available, which means that you don’t need to import them in order to use them.

In the following paragraphs, each of the included components is explained.

The Nuxt Component

The <Nuxt> component is the component you use to display your page components. Basically, this component gets replaced by what is inside your page components depending on the page that is being shown. Therefore it is important that you add the <Nuxt> component to your layouts.

layouts/default.vue

  1. <template>
  2. <div>
  3. <div>My nav bar</div>
  4. <Nuxt />
  5. <div>My footer</div>
  6. </div>
  7. </template>

Nuxt Components - 图1

The <Nuxt> component can only be used inside layouts.

The <Nuxt> component can take the prop of nuxt-child-key. This prop will be passed to <RouterView> so that your transitions will work correctly inside dynamic pages.

There are 2 ways to handle the internal key prop of <RouterView>.

  1. Use a nuxtChildKey prop on your <Nuxt> component

layouts/default.vue

  1. <template>
  2. <div>
  3. <Nuxt :nuxt-child-key="someKey" />
  4. </div>
  5. </template>
  1. Add the key option in page components as string or function
  1. export default {
  2. key(route) {
  3. return route.fullPath
  4. }
  5. }

The NuxtChild Component

This component is used for displaying the children components in a nested route.

Example:

  1. -| pages/
  2. ---| parent/
  3. ------| child.vue
  4. ---| parent.vue

This file tree will generate these routes:

  1. ;[
  2. {
  3. path: '/parent',
  4. component: '~/pages/parent.vue',
  5. name: 'parent',
  6. children: [
  7. {
  8. path: 'child',
  9. component: '~/pages/parent/child.vue',
  10. name: 'parent-child'
  11. }
  12. ]
  13. }
  14. ]

To display the child.vue component, you have to insert the <NuxtChild> component inside pages/parent.vue:

pages/parent.vue

  1. <template>
  2. <div>
  3. <h1>I am the parent view</h1>
  4. <NuxtChild :foobar="123" />
  5. </div>
  6. </template>

keep-alive

Both, the <Nuxt> component and the <NuxtChild/> component, accept keep-alive and keep-alive-props.

Nuxt Components - 图2

To learn more about keep-alive and keep-alive-props see the vue docs

layouts/default.vue

  1. <template>
  2. <div>
  3. <Nuxt keep-alive :keep-alive-props="{ exclude: ['modal'] }" />
  4. </div>
  5. </template>
  6. <!-- will be converted into something like this -->
  7. <div>
  8. <KeepAlive :exclude="['modal']">
  9. <RouterView />
  10. </KeepAlive>
  11. </div>

pages/parent.vue

  1. <template>
  2. <div>
  3. <NuxtChild keep-alive :keep-alive-props="{ exclude: ['modal'] }" />
  4. </div>
  5. </template>
  6. <!-- will be converted into something like this -->
  7. <div>
  8. <KeepAlive :exclude="['modal']">
  9. <RouterView />
  10. </KeepAlive>
  11. </div>

<NuxtChild> components can also receive properties like a regular Vue component.

  1. <template>
  2. <div>
  3. <NuxtChild :key="$route.params.id" />
  4. </div>
  5. </template>

The NuxtLink Component

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

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

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

The <NuxtLink> component should be used for all internal links. That means for all links to the pages within your site you should use <NuxtLink>. The <a> tag should be used for all external links. That means if you have links to other websites you should use the <a> tag for those.

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

Nuxt Components - 图3

If you want to know more about <RouterLink>, feel free to read the Vue Router documentation for more information.

prefetchLinks

Nuxt.js automatically includes smart prefetching. That means it detects when a link is visible, either in the viewport or when scrolling and prefetches the JavaScript for those pages so that they are ready when the user clicks the link. Nuxt.js only loads the resources when the browser isn’t busy and skips prefetching if your connection is offline or if you only have 2G connection.

Nuxt Components - 图4

Check out this article to learn more about smart prefetching.

Disable prefetching for specific links

However sometimes you may want to disable prefetching on some links if your page has a lot of JavaScript or you have a lot of different pages that would be prefetched or you have a lot of third party scripts that need to be loaded. To disable the prefetching on a specific link, you can use the no-prefetch prop. Since Nuxt.js v2.10.0, you can also use the prefetch prop set to false

  1. <NuxtLink to="/about" no-prefetch>About page not prefetched</NuxtLink>
  2. <NuxtLink to="/about" :prefetch="false">About page not prefetched</NuxtLink>

Disable prefetching globally

To disable the prefetching on all links, set the prefetchLinks to false:

nuxt.config.js

  1. export default {
  2. router: {
  3. prefetchLinks: false
  4. }
  5. }

Since Nuxt.js v2.10.0, if you have to set prefetchLinks to false but you want to prefetch a specific link, you can use the prefetch prop:

  1. <NuxtLink to="/about" prefetch>About page prefetched</NuxtLink>

Link Classes

linkActiveClass

The linkActiveClass works the same as the vue-router class for active links. If we want to show which links are active all you have to do is create some css for the class nuxt-link-active .

  1. .nuxt-link-active {
  2. color: red;
  3. }

Nuxt Components - 图5

This css can be added to the navigation component or for a specific page or layout or in your main.css file.

If you want to you can also configure the class name to be something else. You can do this by modifying the linkActiveClass in the router property in your nuxt.config.js file.

  1. export default {
  2. router: {
  3. linkActiveClass: 'my-custom-active-link'
  4. }
  5. }

Nuxt Components - 图6

This option is given directly to the vue-router linkActiveClass. See the vue-router docs for more info.

linkExactActiveClass

The linkExactActiveClass works the same as the vue-router class for exact active links. If we want to show which links are active with an exact match all you have to do is create some css for the class nuxt-link-exact-active .

  1. .nuxt-link-exact-active {
  2. color: green;
  3. }

Nuxt Components - 图7

This css can be added to the navigation component or for a specific page or layout or in your main.css file.

If you want to you can also configure the class name to be something else. You con do this by modifying the linkExactActiveClass in the router property in your nuxt.config.js file.

nuxt.config.js

  1. export default {
  2. router: {
  3. linkExactActiveClass: 'my-custom-exact-active-link'
  4. }
  5. }

Nuxt Components - 图8

This option is given directly to the vue-router linkExactActiveClass. See the vue-router docs for more info

linkPrefetchedClass

The linkPrefetchedClass will allow you to add styles for all links that have been prefetched. This is great for testing which links are being prefetched after modifying the default behavior. The linkPrefetchedClass is disabled by default. If you want to enable it you need to add it to the router property in your nuxt-config.js file.

nuxt.config.js

  1. export default {
  2. router: {
  3. linkPrefetchedClass: 'nuxt-link-prefetched'
  4. }
  5. }

Then you can add the styles for that class.

  1. .nuxt-link-prefetched {
  2. color: orangered;
  3. }

The client-only Component

This component is used to purposely render a component only on client-side. To import a component only on the client, register the component in a client-side only plugin.

pages/example.vue

  1. <template>
  2. <div>
  3. <sidebar />
  4. <client-only placeholder="Loading...">
  5. <!-- this component will only be rendered on client-side -->
  6. <comments />
  7. </client-only>
  8. </div>
  9. </template>

Use a slot as placeholder until <client-only /> is mounted on client-side.

pages/example.vue

  1. <template>
  2. <div>
  3. <sidebar />
  4. <client-only>
  5. <!-- this component will only be rendered on client-side -->
  6. <comments />
  7. <!-- loading indicator, rendered on server-side -->
  8. <comments-placeholder slot="placeholder" />
  9. </client-only>
  10. </div>
  11. </template>

Nuxt Components - 图9

Sometimes in server rendered pages $refs inside <client-only> might not be ready even with $nextTick, the trick might be to call $nextTick couple of times:

page.vue

  1. mounted(){
  2. this.initClientOnlyComp()
  3. },
  4. methods: {
  5. initClientOnlyComp(count = 10) {
  6. this.$nextTick(() => {
  7. if (this.$refs.myComp) {
  8. //...
  9. } else if (count > 0) {
  10. this.initClientOnlyComp(count - 1);
  11. }
  12. });
  13. },
  14. }

Nuxt Components - 图10

If you are using a version of Nuxt < v2.9.0, use <no-ssr> instead of <client-only>