Vue防止页面地址乱输入
我们首先要判断当前这个路由是否存在,如果不存在也就是说(to.matched.length == 0)的话,进行一个判断,当前路由的name为真的话就让路由为他本身的name,如果name为假的话,让他跳转到登录页面。
router.beforeEach((to, from, next) => {
if (to.matched.length == 0) {
from.name ? next({
name: from.name
}) : next('/login')
} else {
if (to.name != 'login') {
if (sessionStorage.getItem('uid') != null) {
next()
} else {
next('/login')
}
} else {
next()
}
}
})