useError

源文件
useError composable returns the global Nuxt error that is being handled.

使用

The useError composable returns the global Nuxt error that is being handled and is available on both client and server. It provides a reactive, SSR-friendly error state across your app.

const error = useError()

You can use this composable in your components, pages, or plugins to access or react to the current Nuxt error.

类型

interface NuxtError<DataT = unknown> {
  status: number
  statusText: string
  message: string
  data?: DataT
  error?: true
}

export const useError: () => Ref<NuxtError | undefined>

参数

This composable does not take any parameters.

返回值

Returns a Ref containing the current Nuxt error (or undefined if there is no error). The error object is reactive and will update automatically when the error state changes.

示例

<script setup lang="ts">
const error = useError()

if (error.value) {
  console.error('Nuxt error:', error.value)
}
</script>
阅读更多内容请参见 文档 > 4 X > 入门 > 错误处理