vue路由登录拦截

 1 import Vue from 'vue'
 2 import Router from 'vue-router'
 3 Vue.use(Router)
 4 import Index from '@/page/index'
 5 
 6 let router = new Router({
 7   routes: [{
 8     path: '/',
 9     name: "index",
10     component: Index,
11   }]
12 })
13 
14 // 路由守卫
15 router.beforeEach((to, from, next) => {
16   // 查找是否存在eleToke,登录的时候保存到localStorage里面的
17   let isLogin = localStorage.eleToke ? true : false //如果存在返回真,如果不存在返回假
18   if (to.path == "/Login" || to.path == "/register") { //如果用户本来是想去登录页或者注册页
19     next() //放行
20   } else {
21     isLogin ? next() : next("/Login") //如果isLogin是真允许正常跳转,如果为假,回到登录页面去
22   }
23 })
24 
25 export default router