Nuxt 提供了 <NuxtClientFallback>
组件,如果其任何子组件在 SSR 中触发错误,则会在客户端渲染其内容。
app/pages/example.vue
<template>
<div>
<Sidebar />
<!-- this component will be rendered on client-side -->
<NuxtClientFallback fallback-tag="span">
<Comments />
<BrokeInSSR />
</NuxtClientFallback>
</div>
</template>
事件
@ssr-error
: 当子组件在 SSR 中触发错误时发出的事件。请注意,这只会在服务器上触发。<template> <NuxtClientFallback @ssr-error="logSomeError"> <!-- ... --> </NuxtClientFallback> </template>
属性
placeholderTag
|fallbackTag
: 指定一个备用标签,如果插槽在服务器上渲染失败则渲染该标签。- 类型:
string
- 默认:
div
- 类型:
placeholder
|fallback
: 指定备用内容,如果插槽渲染失败则渲染该内容。- 类型:
string
- 类型:
keepFallback
: 如果服务器端渲染失败,则保留备用内容。- 类型:
boolean
- 默认:
false
- 类型:
<template>
<!-- render <span>Hello world</span> server-side if the default slot fails to render -->
<NuxtClientFallback
fallback-tag="span"
fallback="Hello world"
>
<BrokeInSSR />
</NuxtClientFallback>
</template>
插槽
#fallback
: 指定在插槽渲染失败时在服务器端显示的内容。
<template>
<NuxtClientFallback>
<!-- ... -->
<template #fallback>
<!-- this will be rendered on server side if the default slot fails to render in ssr -->
<p>Hello world</p>
</template>
</NuxtClientFallback>
</template>