聊聊Vue2和Vue3中怎么设置404界面

 2765

本篇文章带大家进行Vue学习,聊聊Vue2和Vue3中设置404界面的方法,希望对大家有所帮助!


聊聊Vue2和Vue3中怎么设置404界面


vue页面中,如果跳转了不存在的路由那么,那么页面就会出现白屏状态,为了解决这个问题,我们可以自己写一个404界面,让其跳转过去。

一.vue2

1、index.js

在此文件中,一般存放的都是我们的路由信息,我们经常使用path:'*'来进行捕获我们的路由,度过不存在那么我们就让它进行跳转至我们自定义的404页面。

  1. import Vue from 'vue'
  2. import Router from 'vue-router'
  3. import Homepage from '@/components/Homepage'
  4. Vue.use(Router)
  5.  
  6. export default new Router({
  7.   routes: [
  8.     {
  9.       path: '/',
  10.       component: Homepage,
  11.     },
  12.     {
  13.       path:'*',
  14.       component:()=>import('../views/404.vue')
  15.     }
  16.   ]
  17. })

注意:这个path一定要写在最外面。


2、404.vue页面

这个页面通常我们可以自定义,一般包括跳转某个页面的超链接或者就是定时多长时间进行跳转。

  1. <template>
  2.     <div>
  3.         <p>
  4.             页面将在<span>{{ time }}</span>秒后自动跳转首页,<br>
  5.             您也可以点击这里跳转<a href="/">首页</a>
  6.         </p>
  7.     </div>
  8. </template>
  9.  
  10. <script>
  11. // 这里可以导入其他文件(比如:组件,工具js,第三方插件js,json文件,图片文件等等)
  12.  
  13. export default {
  14.     name: '',
  15.     components: {
  16.  
  17.     },
  18.     // 定义属性
  19.     data() {
  20.         return {
  21.             time: '10',
  22.             time_end: null
  23.         }
  24.     },
  25.     // 计算属性,会监听依赖属性值随之变化
  26.     computed: {},
  27.     // 监控data中的数据变化
  28.     watch: {},
  29.     // 方法集合
  30.     methods: {
  31.         GoIndex() {
  32.             let _t = 9
  33.             this.time_end = setInterval(() => {
  34.                 if (_t !== 0) {
  35.                     this.time = _t--;
  36.                 } else {
  37.                     this.$router.replace('/')
  38.                     clearTimeout(this.time_end)
  39.                     this.time_end = null
  40.                 }
  41.             }, 1000)
  42.         }
  43.     },
  44.     // 生命周期 - 创建完成(可以访问当前this实例)
  45.     created() {
  46.         this.GoIndex()
  47.     },
  48.     // 生命周期 - 挂载完成(可以访问DOM元素)
  49.     mounted() {
  50.  
  51.     },
  52.     beforeCreate() { }, // 生命周期 - 创建之前
  53.     beforeMount() { }, // 生命周期 - 挂载之前
  54.     beforeUpdate() { }, // 生命周期 - 更新之前
  55.     updated() { }, // 生命周期 - 更新之后
  56.     beforeDestroy() { }, // 生命周期 - 销毁之前
  57.     destroyed() {
  58.         clearTimeout(this.time_end)
  59.         this.time_end = null
  60.     }, // 生命周期 - 销毁完成
  61.     activated() { }, // 如果页面有keep-alive缓存功能,这个函数会触发
  62. }
  63. </script>
  64.  
  65. <style scoped>
  66. .not_found {
  67.     width: 100%;
  68.     height: 100%;
  69.     background: url('../../static/img/404.gif') no-repeat;
  70.     background-position: center;
  71.     background-size: cover;
  72.  
  73.     p {
  74.         position: absolute;
  75.         top: 50%;
  76.         left: 20%;
  77.         transform: translate(-50%, 0);
  78.         color: #fff;
  79.         span{
  80.             color: orange;
  81.             font-family: '仿宋';
  82.             font-size: 25px;
  83.         }
  84.         a {
  85.             font-size: 30px;
  86.             color: aqua;
  87.             text-decoration: underline;
  88.         }
  89.     }
  90. }
  91. </style>

那么实现的效果就如下图所示:


聊聊Vue2和Vue3中怎么设置404界面

404实现效果


二.vue3

为什么要分开说呢?因为在vue3中我们进行如下设置:

  1. {
  2.      path:'*',
  3.      component:()=>import('../views/404.vue')
  4. }

会产生错误,错误信息:Catch all routes ("*") must now be defined using a param with a custom regexp.,意思就是:现在必须使用与自定义Regexp的参数定义所有路由(“*”)。

那么官方是这么说的:

  1. // plan on directly navigating to the not-found route using its name
  2. { path: '/:pathMatch(.*)*', name: 'not-found', component: NotFound },
  3. // if you omit the last `*`, the `/` character in params will be encoded when resolving or pushing
  4. { path: '/:pathMatch(.*)', name: 'bad-not-found', component: NotFound },

那么我们vue2中的index.js文件中的代码就变成了如下:

  1. ...
  2.  
  3. export default new Router({
  4.   routes: [
  5.     ...,
  6.     {
  7.       path:'/:pathMatch(.*)*',
  8.       component:()=>import('../views/404.vue')
  9.     }
  10.     //或者
  11.      {
  12.       path:'/:pathMatch(.*)',
  13.       component:()=>import('../views/404.vue')
  14.     }
  15.   ]
  16. })


TAG标签:
本文网址:https://www.zztuku.com/index.php/detail-13836.html
站长图库 - 聊聊Vue2和Vue3中怎么设置404界面
申明:本文转载于《CSDN》,如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐