error.vue 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <template>
  2. <div class="errorBox">
  3. <img src="./public/error/404.png" alt="页面不存在" v-if="errorCode == 404">
  4. <img src="./public/error/error.png" alt="服务器渲染错误" v-if="errorCode == 500">
  5. <div class="errorText" v-if="errorCode == 404">对不起,您访问的页面不存在({{ errorText }})</div>
  6. <div class="errorText" v-if="errorCode == 500">服务器渲染错误({{ errorText }})</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. //1.处理错误 end ---------------------------------------->
  22. //2.处理跳转 start ---------------------------------------->
  23. const router = useRouter()
  24. const goHome = () => {
  25. router.push('/')
  26. }
  27. //2.处理跳转 end ---------------------------------------->
  28. </script>
  29. <style lang="less" scoped>
  30. .errorBox {
  31. width: 100%;
  32. text-align: center;
  33. padding-top: 150px;
  34. img {
  35. width: 404px;
  36. height: 302px;
  37. margin: 0 auto;
  38. }
  39. .goHome {
  40. width: 120px;
  41. height: 32px;
  42. line-height: 32px;
  43. font-size: 14px;
  44. color: #20242E;
  45. text-align: center;
  46. margin: 0 auto;
  47. background: #fff;
  48. border: 1px solid #D4D6D9;
  49. border-radius: 2px;
  50. cursor: pointer;
  51. }
  52. .errorText {
  53. font-size:16px;
  54. color: #727785;
  55. margin-top: 40px;
  56. margin-bottom: 20px;
  57. }
  58. }
  59. </style>