vue路由url不存在则跳转指定页面

  • 方法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' }
  ]
})

版权声明:本文为EnidChann原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。