简体   繁体   English

如何在 NuxtJS 中只渲染一次组件?

[英]How to render components only once in NuxtJS?

In Nuxt Framework using SPA mod, I have a default layout:在使用 SPA mod 的 Nuxt 框架中,我有一个默认布局:

<template>
  <div id="wrapper">
    <my-header />
    <nuxt />
    <my-footer />
  </div>
</template>

In the example above, <my-header> and <my-footer> are the components I created.在上面的示例中, <my-header><my-footer>是我创建的组件。

I want them to initialize and render only once.我希望他们只初始化和渲染一次。 After changing the router, they will not render again.更改路由器后,它们将不再渲染。

Assuming the code you provided is located in layouts > default.vue, for example, and that you did not change the default settings within the .nuxt folder, it will render only once.例如,假设您提供的代码位于 layouts > default.vue 中,并且您没有更改.nuxt文件夹中的默认设置,它只会呈现一次。

Only what is within the <nuxt /> will change - that will be the content of your app in general.只有<nuxt />中的内容会改变 - 这将是您应用程序的一般内容。 Basically it will display the page components (used only in layouts - ref ).基本上它将显示页面组件(仅在布局中使用 - ref )。

So in this case, as long as you do not refresh the page the header and footer will not be rerendered.所以在这种情况下,只要不刷新页面,header 和页脚就不会被重新渲染。 When moving between the pages you might want to use nuxt-link ( ref ) and not force the reload (leave it at default settings).在页面之间移动时,您可能希望使用nuxt-link ( ref ) 而不是强制重新加载(将其保留为默认设置)。

Example nuxt-link usage:示例nuxt-link用法:

<nuxt-link to="/about">About page</nuxt-link>

The about page would be located in:关于页面将位于:

pages > about > index.vue页面 > 关于 > index.vue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM