import { createRouter, createWebHashHistory } from 'vue-router'

//路由规则,如下

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/',
    name: '',
    component: () => import('../views/Login.vue'),
    redirect:{name:'login'}
  }]

 

//创建路由实例

const router = createRouter({
  history: createWebHashHistory(),
  routes    //路由规则
})

// 导航守卫
router.beforeEach(async (to, from) => {
  // 判断session里面是否有登录信息
  const isAuthenticated = window.sessionStorage.getItem('user')
  if (
    // 检查用户是否已登录
    !isAuthenticated &&
    // 避免无限重定向
    to.name !== 'login'
  ) {
    // 将用户重定向到登录页面
    return { name: 'login' }
  }
})
 
export default router
posted on 2023-05-11 10:41  给天使看的戲  阅读(17)  评论(0编辑  收藏  举报