欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

解決vue項(xiàng)目路徑不正確,自動(dòng)跳轉(zhuǎn)404的問(wèn)題

 更新時(shí)間:2022年10月09日 08:38:00   作者:不另外加糖  
這篇文章主要介紹了解決vue項(xiàng)目路徑不正確,自動(dòng)跳轉(zhuǎn)404的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

vue項(xiàng)目路徑不正確,自動(dòng)跳轉(zhuǎn)404

第一種方法

使用vuerouter鉤子函數(shù)beforeEach,每次進(jìn)行路由跳轉(zhuǎn)時(shí)都進(jìn)行判斷,若頁(yè)面不存在就跳轉(zhuǎn)到404頁(yè)面。

import Error from ' '
?
const router = new Router({
? ? routes:[
? ? ? ? name: 'error',
? ? ? ? path: '/error',
? ? ? ? component: Error
? ? ]
}
?
//beforeEach每次進(jìn)行路由跳轉(zhuǎn)時(shí)都會(huì)執(zhí)行
router.beforeEach((to,from,next){
? ? if(to.matched.length === 0){
? ? ? ? from.path ? next({name: from.name}) : next('/error')
? ? }else{
? ? ? ? next()
? ? }
})
?
export default router

第二種

redirect重定向

?{ ? ? path: '/404', ? ? ??
? ? ? ?component: () => import('@/views/error-page/404'), ? ? ??
? ? ? ?hidden: true ? ??
? },?
? ??
//這個(gè)*匹配必須放在最后,將改路由配置放到所有路由的配置信息的最后,否則會(huì)其他路由path匹配造成影響。 ? ??
?{ ? ? path: '*',
? ? ? ?redirect: '/404',?
? ? ? ?hidden: true?
? }

vue路由判斷跳轉(zhuǎn)404頁(yè)面

beforeEach函數(shù) 這是路由跳轉(zhuǎn)前處理的函數(shù)

import PageNotFound from '@/views/pages/404.vue'
Vue.use(Router)
const routes=[
? {
? ? path: '*',
? ? name: 'PageNotFound',
? ? component: PageNotFound,
? },
]?
?
const router = new Router({
? mode: 'history',
? routes: routes
})
?
router.beforeEach((to, from, next) => {
? // ?從其他地方訪(fǎng)問(wèn)是否有這個(gè)地址
? ? if(to.matched.length == 0){
? ? ? from.path ? next({name: from.name}) : next('*')
? ? }
? ? next();
});

第二種方法就是重定向地址 同上

修改routes中的地址

? {
? ? path: '/404',
? ? name: 'PageNotFound',
? ? component: PageNotFound,
? },
? {
? ? path:'*',
? ? redirect:'/404'
? ?}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。 

相關(guān)文章

最新評(píng)論