透過 100 多個技巧的集合來學習 Nuxt!

<ClientOnly>

使用 <ClientOnly> 元件僅在客戶端渲染元件。

<ClientOnly> 元件用於刻意僅在客戶端渲染元件。

預設插槽的內容將會從伺服器建置中進行搖樹最佳化。(這表示當渲染初始 HTML 時,其中元件所使用的任何 CSS 可能不會被內嵌。)

屬性

  • placeholderTag | fallbackTag: 指定要在伺服器端渲染的標籤。
  • placeholder | fallback: 指定要在伺服器端渲染的內容。
<template>
  <div>
    <Sidebar />
    <!-- The <Comment> component will only be rendered on client-side -->
    <ClientOnly fallback-tag="span" fallback="Loading comments...">
      <Comment />
    </ClientOnly>
  </div>
</template>

插槽

  • #fallback: 指定要在伺服器上渲染的內容,並在瀏覽器中掛載 <ClientOnly> 之前顯示。
pages/example.vue
<template>
  <div>
    <Sidebar />
    <!-- This renders the "span" element on the server side -->
    <ClientOnly fallbackTag="span">
      <!-- this component will only be rendered on client side -->
      <Comments />
      <template #fallback>
        <!-- this will be rendered on server side -->
        <p>Loading comments...</p>
      </template>
    </ClientOnly>
  </div>
</template>