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

解決vue項目路徑不正確,自動跳轉(zhuǎn)404的問題

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

vue項目路徑不正確,自動跳轉(zhuǎn)404

第一種方法

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

import Error from ' '
?
const router = new Router({
? ? routes:[
? ? ? ? name: 'error',
? ? ? ? path: '/error',
? ? ? ? component: Error
? ? ]
}
?
//beforeEach每次進行路由跳轉(zhuǎn)時都會執(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 ? ??
? },?
? ??
//這個*匹配必須放在最后,將改路由配置放到所有路由的配置信息的最后,否則會其他路由path匹配造成影響。 ? ??
?{ ? ? path: '*',
? ? ? ?redirect: '/404',?
? ? ? ?hidden: true?
? }

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

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) => {
? // ?從其他地方訪問是否有這個地址
? ? if(to.matched.length == 0){
? ? ? from.path ? next({name: from.name}) : next('*')
? ? }
? ? next();
});

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

修改routes中的地址

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

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

相關(guān)文章

最新評論