vue 中编写404页面

前言

今日给自己项目添加404页面时,踩了一点坑,分享给大家。

正文
    <div class="_404">
        <h2 class="m-0">抱歉,页面未找到,<span>{{countDown}}</span>s后自动跳转到
            <a href="javascript:;" @click="goHome">首页</a>
        </h2>
        <img src="../assets/404.jpg" alt="页面未找到">
    </div>
    
    <style>
	    body {
	        background-color: #dceebc;
	    }
	
	    ._404 {
	        width: 30%;
	        margin: 5rem auto;
	    }
	
	    ._404 img {
	        width: 30rem;
	    }
	
	    ._404 a {
	        color: #010101;
	    }
	
	    ._404 a:hover {
	        color: skyblue;
	    }
</style>

在这里插入图片描述

页面结构很简单,一点提示信息和一张顺眼的图片即可。

<script>
    export default {
        name: "NF404",
        data() {
            return {
                countDown: 5
            }
        },
        methods: {
            goHome() {
                this.$router.push('/main/home')
            }
        },
        created() {
            setInterval(() => {
                this.countDown > 0 ? this.countDown-- : this.$router.push('/main/home')
            }, 1000)
        }
    }
</script>

脚本这里,逻辑为倒计时后自动跳转至首页,或者主动点击链接也可以跳转。

  • 结果发现,第一次进入404页面,是很成功的,但是再次进入的话,就会直接跳转至主页,没有倒计时。
  • 第一时间我是觉得是因为vue组件从缓存里加载的问题,所以created函数只在第一次触发。于是在created函数中加入了alert(),发现每次进入404页面,确实是重新创建了。也就是说,vue router变换时,默认是会销毁离开的组件的。
  • 那么我又想到可能是setInterval()的问题,可能是没有及时清除的原因,将代码修改之后,成功运行。
<script>
    export default {
        name: "NF404",
        data() {
            return {
                countDown: 5,
                timer: null
            }
        },
        methods: {
            goHome() {
                this.$router.push('/main/home')
                clearInterval(this.timer)
            }
        },
        created() {
            this.timer = setInterval(() => {
                this.countDown > 0 ? this.countDown-- : this.goHome()
            }, 1000)
        }
    }
</script>
22/05/10更新

在vue2中,捕获404路径的写法是:

{
	path: "*",
    name: "NotFound",
    component: () => import("@/views/common/not-found/NotFound.vue"),
}

而在vue3中,vue-router禁用了这种写法,改为:

{
	path: "/:pathMatch(.*)",
    name: "NotFound",
    component: () => import("@/views/common/not-found/NotFound.vue"),
}
结语

在vue组件中使用计时器的时候,一定要记得及时清除!
如果对你有帮助的话,请点一个赞吧

  • 13
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
当使用Vue和ElementUI编写404页面时,你可以按照以下步骤进行操作: 1. 创建一个名为`NotFound.vue`的组件文件,并在其编写404页面的代码: ```vue <template> <div class="not-found"> <h1>404 - 页面不存在</h1> <p>抱歉,您访问的页面不存在。</p> </div> </template> <style scoped> .not-found { text-align: center; padding: 100px; } h1 { font-size: 36px; } p { font-size: 18px; color: #999; } </style> ``` 2. 在你的路由文件(通常是`router/index.js`)设置404页面的路由: ```javascript import Vue from 'vue'; import Router from 'vue-router'; // 导入 NotFound 组件 import NotFound from '@/components/NotFound.vue'; Vue.use(Router); const router = new Router({ mode: 'history', routes: [ // 其他路由配置... // 添加404路由配置 { path: '*', component: NotFound, }, ], }); export default router; ``` 3. 在你的入口文件(通常是`main.js`)导入ElementUI相关组件和样式: ```javascript import Vue from 'vue'; import ElementUI from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; Vue.use(ElementUI); ``` 4. 在你的根组件(通常是`App.vue`)使用路由出口渲染404页面: ```vue <template> <div id="app"> <router-view></router-view> </div> </template> ``` 现在,当用户访问不存在的页面时,将会显示404页面。你可以根据实际需求对404页面进行样式调整和内容修改。记得安装Vue、ElementUI和Vue Router相关的依赖,然后运行你的Vue项目。希望能对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值