error.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <template>
  2. <div class="errorBox">
  3. <img src="http://img.bjzxtw.org.cn/pre/image/jpeg/20250701/1751346180738003.jpg" alt="页面不存在" v-if="errorCode == 404">
  4. <img src="http://img.bjzxtw.org.cn/pre/image/jpeg/20250701/1751346208700657.jpg" alt="服务器渲染错误" v-if="errorCode == 500">
  5. <div class="errorText" v-if="errorCode == 404">对不起,您访问的页面不存在</div>
  6. <div class="errorText" v-if="errorCode == 500">服务器渲染错误</div>
  7. <div @click="goHome" class="goHome">返回网站首页</div>
  8. </div>
  9. </template>
  10. <script setup>
  11. //1.处理错误 start ---------------------------------------->
  12. //获得错误信息
  13. const error = useError();
  14. //console.log('错误信息:', error.value);
  15. //获得错误代码与错误文字
  16. const errorCode = ref(0)
  17. const errorText = ref('')
  18. errorCode.value = error.value.statusCode;
  19. errorText.value = error.value.message;
  20. console.log('错误代码:', errorCode.value);
  21. console.log('错误明细:', errorText.value);
  22. //1.处理错误 end ---------------------------------------->
  23. //2.处理跳转 start ---------------------------------------->
  24. const router = useRouter()
  25. const goHome = () => {
  26. router.push('/')
  27. }
  28. //2.处理跳转 end ---------------------------------------->
  29. </script>
  30. <style lang="less" scoped>
  31. .errorBox {
  32. width: 100%;
  33. text-align: center;
  34. padding-top: 150px;
  35. img {
  36. width: 404px;
  37. height: 302px;
  38. margin: 0 auto;
  39. }
  40. .goHome {
  41. width: 120px;
  42. height: 32px;
  43. line-height: 32px;
  44. font-size: 14px;
  45. color: #20242E;
  46. text-align: center;
  47. margin: 0 auto;
  48. background: #fff;
  49. border: 1px solid #D4D6D9;
  50. border-radius: 2px;
  51. cursor: pointer;
  52. }
  53. .errorText {
  54. font-size:16px;
  55. color: #727785;
  56. margin-top: 40px;
  57. margin-bottom: 20px;
  58. }
  59. }
  60. </style>