可以使用 router.beforeEach 注册一个全局前置守卫:
//前置守卫
router.beforeEach((to, from, next) => {
//路由跳转中
if (to.path == "/") {
next();
} else {
if (window.localStorage.getItem("loginUserInfo")) {
next()
} else {
next({
path: '/'})
}
}
})