guhongwei hace 5 años
padre
commit
8ec6a09fef
Se han modificado 2 ficheros con 10 adiciones y 0 borrados
  1. 8 0
      src/router/before.js
  2. 2 0
      src/router/index.js

+ 8 - 0
src/router/before.js

@@ -2,6 +2,14 @@ import store from '@/store/index';
 
 const checkLogin = (router, func) => {
   router.beforeEach(async (to, form, next) => {
+    if (to.name == 'index') {
+      let token = localStorage.getItem('token');
+      if (token) {
+        next();
+      } else {
+        next({ name: 'login' });
+      }
+    }
     let res = await store.dispatch('login/toGetUser', func ? func : null);
     next();
   });

+ 2 - 0
src/router/index.js

@@ -7,6 +7,7 @@ Vue.use(VueRouter);
 const routes = [
   {
     path: '/',
+    name: 'index',
     meta: { title: '首页' },
     component: () => import('../views/index.vue'),
   },
@@ -82,6 +83,7 @@ const routes = [
   },
   {
     path: '/login',
+    name: 'login',
     meta: { title: '登录' },
     component: () => import('../views/login.vue'),
   },