- 方法1:路由守卫拦截判断
router.beforeEach((to, from, next) => {
if (to.matched.length === 0) { // 如果未匹配到路由
from.name ? next({ name: from.name }) : next('/login')
} else {
next() // 如果匹配到正确跳转
}
})
- 方法2:对路由做重定向
// routes里加上 { path: '*',redirect: '/login' },如下
const router = new Router({
mode: 'history',
routes: [
{...}
{...}
{...}
{ path: '*',redirect: '/login' }
]
})