VUE项目后台管理系统(三)退出功能,清除cookie,跳转到首页,控制有cookie才可以进其他页面

2021-06-17 19:53:10 浏览数 (1)

目录

  • 路由导航守卫

路由导航守卫

代码语言:javascript复制
import Vue from 'vue'
import VueRouter from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Login from '@/components/Login'

Vue.use(VueRouter)

const routes=
 [
    {
      path: '/',
      name: 'HelloWorld',
      redirect: '/login',
      component: HelloWorld
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/home',
      name: 'home',
      component: HelloWorld
    },
  ]
 const router =new VueRouter({
   routes
 })


// 路由导航守卫

router.beforeEach((to,form,next)=>{
  if(to.path == '/login'){
    next()
  }else{
    const token = window.sessionStorage.getItem('token');
    if(!token){
        next('/login')
    }else{
        next()
    }


  }
})


export default router

0 人点赞