非法请求显示404页面

用户在访问不存在路由时,会显示404页面,用户可自行切换页面或回到首页

404页面作为一个通用页面,任何非法请求,全部都会跳转到404页面上

###

应用到的技术栈

vue3 -- vue-router -- vite -- less

###

进入正题了

###

这里是需要用到的图片

                               

###

PC端编写

首先在views里创建文件夹404,例如404/index.vue

页面搭建
<template>
    <div class="undefind">
        <h1> 404 <span><a href="#" @click="router.push('/home')">点我回首页😊</a></span>
            <h2>当前页面还在开发中,敬请期待......</h2>
        </h1>
    </div>
</template>

<script setup>
import { useRouter } from 'vue-router'
const router = useRouter()
</script>

<style lang="less" scoped>
* {
    margin: 0;
    padding: 0;
}

.undefind {
    background-image: url('../../assets/01da295b2749baa8012034f792b59f.jpg@2o.jpg');
    height: 100vh;
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    display: flex;
    justify-content: center;
    align-items: center;

    h1 {
        text-align: center;
        margin-top: 600px;

        a {
            text-decoration: none;
            color: red;
        }
    }
}
</style>
路由配置
import { createRouter, createWebHistory } from "vue-router";
import Home from "../views/Home.vue";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "login",
      component: () => import("../views/Login.vue"),
    },
    {
      path: "/home",
      name: "home",
      component: Home,
    },
    
    // 关键在这里,这里是需要配置的路由
    {
      path: "/404",
      component: () => import("../views/404/index.vue"),
      name: "404",
    },
    {
      path: "/:pathMatch(.*)",
      // 重定向
      redirect: "/404",
      name: "any",
    },
  ],
});

export default router;

###

APP端编写

app的编写和pc的一样,可以看着写一下

页面搭建
<template>
    <div class="undefind">
        <h3><span><a href="#" @click="router.push('/home')">点我回首页😊</a></span>
            <h5>页面开发中,敬请期待......</h5>
        </h3>
    </div>
</template>

<script setup>
import { useRouter } from 'vue-router'
const router = useRouter()
</script>

<style lang="less" scoped>
* {
    margin: 0;
    padding: 0;
}

.undefind {
    background-image: url('../../assets/01b64e5bcec3b5a8012099c88a6e0b.png');
    height: 100vh;
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    display: flex;
    justify-content: center;
    align-items: center;

    h3 {
        text-align: center;
        margin-top: 550px;

        a {
            text-decoration: none;
            color: red;
        }
    }
}
</style>
路由配置
import { createRouter, createWebHistory } from "vue-router";
import Home from "../views/Home.vue";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "login",
      component: () => import("../views/Login.vue"),
    },
    {
      path: "/home",
      name: "home",
      component: Home,
    },
    // 配置404页面路由
    {
      path: "/404",
      component: () => import("../views/404/index.vue"),
      name: "404",
    },
    {
      path: "/:pathMatch(.*)",
      redirect: "/404",
      name: "any",
    },
  ],
});

export default router;

  • 20
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值