【问题标题】:Nuxt how to access the exception error on the server error page?Nuxt如何访问服务器错误页面的异常错误?
【发布时间】:2022-08-19 16:37:54
【问题描述】:

在 Nuxt.js 中,有没有办法全局处理任何服务器端错误?

我想有条件地重定向到我自己的 Vue 错误页面。例如:

if (process.env.NODE_ENV === \'production\') {
   redirect(\"/error?message=the-error-message-here\");
} else {
   // render the default server error page with the stack trace
}

这可能吗?

我读过有关创建app/views/error.html 的信息,但这并没有改变任何东西。

标签: nuxt.js


【解决方案1】:

你应该把error.vue放在这里

布局/error.vue

    <template>
      <div>
        <h1 v-if="error.statusCode === 404">Page not found</h1>
        <h1 v-else>An error occurred</h1>
        <NuxtLink to="/">Home page</NuxtLink>
      </div>
    </template>

<script>
  export default {
    props: ['error'],
    layout: 'error' // you can set a custom layout for the error page
  }
</script>

您必须将此布局视为发生错误(404、500 等)时显示的组件。与其他页面组件类似,您也可以按照通常的方式为错误页面设置自定义布局。

这是文档: https://nuxtjs.org/docs/concepts/views#error-page

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    • 2021-12-29
    • 2018-11-01
    • 1970-01-01
    • 2016-11-11
    相关资源
    最近更新 更多