guhongwei hace 4 años
padre
commit
dba955aed9
Se han modificado 2 ficheros con 22 adiciones y 3 borrados
  1. 7 2
      src/router/before.js
  2. 15 1
      src/router/index.js

+ 7 - 2
src/router/before.js

@@ -2,8 +2,13 @@ import store from '@/store/index';
 
 const checkLogin = (router, func) => {
   router.beforeEach(async (to, form, next) => {
-    let res = await store.dispatch('login/toGetUser', func ? func : null);
-    next();
+    if (to.name == 'user_index') {
+      let res = await store.dispatch('login/toGetUser', func ? func : null);
+      next();
+    } else {
+      // console.log('2');
+      next();
+    }
   });
 };
 export default checkLogin;

+ 15 - 1
src/router/index.js

@@ -72,7 +72,21 @@ const router = new VueRouter({
   base: process.env.NODE_ENV === 'development' ? '' : process.env.VUE_APP_ROUTER,
   routes,
 });
-checkLogin(router);
+
+router.beforeEach(async (to, form, next) => {
+  if (to.name == 'user_index') {
+    let res = await store.dispatch('login/toGetUser');
+    if (res) {
+      next();
+    } else {
+      next({ name: 'login' });
+    }
+  } else {
+    next();
+  }
+});
+
+// checkLogin(router);
 const originalPush = VueRouter.prototype.push;
 VueRouter.prototype.push = function push(location, onResolve, onReject) {
   if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject);