一、項目啟動后,輸入localhost:8080直接跳轉到404
修改路由
router/index.js
{
path: '/404',
name: '404',
component: () => import('../views/404.vue')
},
// 路由守衛(wèi)
router.beforeEach((to, from, next) => {
localStorage.setItem("currentPathName", to.name) // 設置當前的路由名稱,為了在Header組件中去使用
store.commit("setPath") // 觸發(fā)store的數(shù)據(jù)更新
//未找到路由的情況
if (!to.matched.length) {
const storeMenus = localStorage.getItem("menus")
if (storeMenus) {
next("/404")
} else {
//跳回登錄頁面
next("/login")
}
}
//其他的情況
next()
})
二、個人信息頁面404
router/index.js
const manageRoute = {
path: '/',
name: 'Manage',
component: () => import('../views/Manage.vue'),
redirect: "/home",
children: [
{
path: '/person',
name: '個人信息',
component: () => import('../views/Person.vue'),
},
]
}
三、新建頁面404
1.新建頁面
2.頁面添加菜單
3.給管理員分配菜單
3.重新登錄
4.點進剛分配的菜單
5.404
router/index.js
//提供一個重置路由的方法
export const resetRouter=()=>{
router.matcher = new VueRouter({
mode:'history',
base:process.env.BASE_URL,
routes
})
}
store/index.js文章來源:http://www.zghlxwxcb.cn/news/detail-535283.html
logout(){
localStorage.removeItem("user")
localStorage.removeItem("menus")
router.push("/login")
//重置路由
resetRouter()
}
優(yōu)化創(chuàng)建路由,防止頻繁創(chuàng)建文章來源地址http://www.zghlxwxcb.cn/news/detail-535283.html
//注意刷新頁面會導致重置路由
export const setRoutes = () => {
const storeMenus = localStorage.getItem("menus");
if (storeMenus) {
//獲取當前的路由對象名稱數(shù)組
const currentRouteNames = router.getRoutes().map(v => v.name)
if (!currentRouteNames.includes('Manage')) {
//拼裝動態(tài)路由
const manageRoute = {
path: '/',
name: 'Manage',
component: () => import('../views/Manage.vue'),
redirect: "/home",
children: [
{
path: '/person',
name: '個人信息',
component: () => import('../views/Person.vue'),
},
]
}
const menus = JSON.parse(storeMenus)
menus.forEach(item => {
//當且僅當path不為空的時候才去設置路由
if (item.path) {
let itemMenu = {
path: item.path.replace("/", ""),
name: item.name,
component: () => import('../views/' + item.pagePath + '.vue')
}
manageRoute.children.push(itemMenu)
} else if (item.children.length) {
item.children.forEach(item => {
if (item.path) {
let itemMenu = {
path: item.path.replace("/", ""),
name: item.name,
component: () => import('../views/' + item.pagePath + '.vue')
}
manageRoute.children.push(itemMenu)
}
})
}
})
//動態(tài)添加到現(xiàn)在的路由對象中去
router.addRoute(manageRoute)
}
}
}
到了這里,關于SpringBoot2+Vue2實戰(zhàn)(十一)bug解決的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!