人妻精品在线观看一区二区三区,蜜臀av精品一区二区三区网站,中文一区二区三区亚洲欧美,熟女人妇精品一区二区,人妻av在线观看视频,欧美日韩国产三级精品网站,黄色免费网站直接进入,超碰公开福利正在播放,国产毛片乡下农村妇女毛片

聊聊Vue2和Vue3中怎么設(shè)置404界面

來(lái)源:php中文網(wǎng) | 2023-02-18 11:49:36 |

本篇文章帶大家進(jìn)行Vue學(xué)習(xí),聊聊Vue2和Vue3中設(shè)置404界面的方法,希望對(duì)大家有所幫助!

vue頁(yè)面中,如果跳轉(zhuǎn)了不存在的路由那么,那么頁(yè)面就會(huì)出現(xiàn)白屏狀態(tài),為了解決這個(gè)問(wèn)題,我們可以自己寫(xiě)一個(gè)404界面,讓其跳轉(zhuǎn)過(guò)去。

一.vue2

1.index.js

在此文件中,一般存放的都是我們的路由信息,我們經(jīng)常使用path:"*"來(lái)進(jìn)行捕獲我們的路由,度過(guò)不存在那么我們就讓它進(jìn)行跳轉(zhuǎn)至我們自定義的404頁(yè)面。


(相關(guān)資料圖)

import Vue from "vue"import Router from "vue-router"import Homepage from "@/components/Homepage"Vue.use(Router)export default new Router({  routes: [    {      path: "/",      component: Homepage,    },    {      path:"*",      component:()=>import("../views/404.vue")    }  ]})

2.404.vue頁(yè)面

這個(gè)頁(yè)面通常我們可以自定義,一般包括跳轉(zhuǎn)某個(gè)頁(yè)面的超鏈接或者就是定時(shí)多長(zhǎng)時(shí)間進(jìn)行跳轉(zhuǎn)。

<template>    <div>        <p>            頁(yè)面將在<span>{{ time }}</span>秒后自動(dòng)跳轉(zhuǎn)首頁(yè),<br>            您也可以點(diǎn)擊這里跳轉(zhuǎn)<a href="/">首頁(yè)</a>        </p>    </div></template><script>// 這里可以導(dǎo)入其他文件(比如:組件,工具js,第三方插件js,json文件,圖片文件等等)export default {    name: "",    components: {    },    // 定義屬性    data() {        return {            time: "10",            time_end: null        }    },    // 計(jì)算屬性,會(huì)監(jiān)聽(tīng)依賴屬性值隨之變化    computed: {},    // 監(jiān)控data中的數(shù)據(jù)變化    watch: {},    // 方法集合    methods: {        GoIndex() {            let _t = 9            this.time_end = setInterval(() => {                if (_t !== 0) {                    this.time = _t--;                } else {                    this.$router.replace("/")                    clearTimeout(this.time_end)                    this.time_end = null                }            }, 1000)        }    },    // 生命周期 - 創(chuàng)建完成(可以訪問(wèn)當(dāng)前this實(shí)例)    created() {        this.GoIndex()    },    // 生命周期 - 掛載完成(可以訪問(wèn)DOM元素)    mounted() {    },    beforeCreate() { }, // 生命周期 - 創(chuàng)建之前    beforeMount() { }, // 生命周期 - 掛載之前    beforeUpdate() { }, // 生命周期 - 更新之前    updated() { }, // 生命周期 - 更新之后    beforeDestroy() { }, // 生命周期 - 銷(xiāo)毀之前    destroyed() {        clearTimeout(this.time_end)        this.time_end = null    }, // 生命周期 - 銷(xiāo)毀完成    activated() { }, // 如果頁(yè)面有keep-alive緩存功能,這個(gè)函數(shù)會(huì)觸發(fā)}</script><style scoped>.not_found {    width: 100%;    height: 100%;    background: url("../../static/img/404.gif") no-repeat;    background-position: center;    background-size: cover;    p {        position: absolute;        top: 50%;        left: 20%;        transform: translate(-50%, 0);        color: #fff;        span{            color: orange;            font-family: "仿宋";            font-size: 25px;        }        a {            font-size: 30px;            color: aqua;            text-decoration: underline;        }    }}</style>

那么實(shí)現(xiàn)的效果就如下圖所示:

404實(shí)現(xiàn)效果

二.vue3

為什么要分開(kāi)說(shuō)呢?因?yàn)樵趘ue3中我們進(jìn)行如下設(shè)置:

{      path:"*",      component:()=>import("../views/404.vue")    }

會(huì)產(chǎn)生錯(cuò)誤,錯(cuò)誤信息:Catch all routes ("*") must now be defined using a param with a custom regexp.,意思就是:現(xiàn)在必須使用與自定義Regexp的參數(shù)定義所有路由(“*”)。

那么官方是這么說(shuō)的:

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

那么我們vue2中的index.js文件中的代碼就變成了如下:

...export default new Router({  routes: [    ...,    {      path:"/:pathMatch(.*)*",      component:()=>import("../views/404.vue")    }    //或者     {      path:"/:pathMatch(.*)",      component:()=>import("../views/404.vue")    }  ]})

(學(xué)習(xí)視頻分享:vuejs入門(mén)教程、編程基礎(chǔ)視頻)

以上就是聊聊Vue2和Vue3中怎么設(shè)置404界面的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!

關(guān)鍵詞: 404界面 Vue2 vue3 vue.js