简体   繁体   English

如何从/ pages重定向到layouts / error.vue?

[英]How to redirect from /pages to layouts/error.vue?

I have created a error.vue file within the layouts folder which should show the UI for a 400, 404, 410 and 500 error. 我在layouts文件夹中创建了一个error.vue文件,该文件应显示400、404、410和500错误的UI。 But, it is not showing the ones I created, it is still showing the default NuxtServerError page. 但是,它没有显示我创建的内容,而是显示了默认的NuxtServerError页面。

So, my question is how can I show my created UI within a page. 因此,我的问题是如何在页面中显示我创建的UI。

Below is the code that I am using within the layouts/error.vue 以下是我在layouts / error.vue中使用的代码

HTML: HTML:

<template>
  <div class="error-container">
    <div class="error-content" v-if="error.statusCode === 404">
      <div>
        <h1>Sorry, the page you were looking for doesn't exist.</h1>
        <p>You can return to our <nuxt-link to="/">home page</nuxt-link> or <a href="mailto:contact@web.co">contact</a> us if you can't find what you are looking for.</p>
      </div>
      <img src="~/assets/Images/404.png">
    </div>

    <div class="error-content" v-if="error.statusCode === 400">
      <div>
        <h1>Sorry, the page you are looking for doesn't exist anymore.</h1>
        <p>You can return to our <nuxt-link to="/">home page</nuxt-link> or <a href="mailto:contact@web.co">contact</a> us if you can't find what you are looking for.</p>
      </div>
      <img src="~/assets/Images/404.png">
    </div>

    <div class="error-content" v-if="error.statusCode === 410">
      <div>
        <h1>Sorry, the page you are looking for has been deleted.</h1>
        <p>You can return to our <nuxt-link to="/">home page</nuxt-link> or <a href="mailto:contact@web.co">contact</a> us if you can't find what you are looking for.</p>
      </div>
      <img src="~/assets/Images/404.png">
    </div>

    <div class="error-content" v-if="error.statusCode === 500">
      <div>
        <h1>Sorry, the page you were looking for doesn't exist.</h1>
        <p>You can return to our <nuxt-link to="/">home page</nuxt-link> or <a href="mailto:contact@web.co">contact</a> us if you can't find what you are looking for.</p>
      </div>
      <img src="~/assets/Images/404.png">
    </div>
  </div>  
</template>

Javascript: 使用Javascript:

<script>
export default {
  head() {
    return {
      title: 'Lost?'
    }
  },
  props: ['error'],
  layout: 'error'
}
</script>

CSS: CSS:

<style scoped>
.error-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}

.error-content {
  width: 90%;
  max-width: 800px;
  margin: auto;
  display: grid;
  grid-template: auto / auto 200px;
  grid-column-gap: 10px;
  text-align: center;
}

.error-content > div {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.error-content > div > h1 {
  font-size: 30px;
  margin: 0;
}

.error-content > img {
  width: 200px;
}

a {
  text-decoration: unset;
  color: var(--color-tpBlue);
}
</style>

Many thanks in advance! 提前谢谢了!

Redirect to error layout does not happen automatically. 重定向到error布局不会自动发生。 Instead, redirect should be done in relevant hooks or methods of your application. 而是应在应用程序的相关挂钩或方法中完成重定向。 It is done by calling error method available in Nuxt context with statusCode and message props passed, which will replace page with with content of layout/error.vue and will respond with HTTP status provided as a statusCode prop. 这是通过调用Nuxt 上下文中可用的error方法(带有statusCodemessage props)来完成的,该方法将使用layout/error.vue内容替换页面,并以statusCode prop提供的HTTP状态进行响应。

Here is an example of handling errors inside asyncData : 这是处理asyncData内部错误的示例

export default {
  async asyncData ({ params, error }) {
    try {
      const { data } = await axios.get(`https://my-api/posts/${params.id}`)
      return { title: data.title }
    } catch (error) {
       error({ statusCode: 404, message: 'Post not found' })
    }
  }
}

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

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