Browse Source

feature: add a redirect url when not logged in

https://github.com/PanJiaChen/vue-element-admin/issues/438
Pan 6 years ago
parent
commit
ccd07c3d63
2 changed files with 12 additions and 3 deletions
  1. 1 1
      src/permission.js
  2. 11 2
      src/views/login/index.vue

+ 1 - 1
src/permission.js

@@ -30,7 +30,7 @@ router.beforeEach((to, from, next) => {
30
     if (whiteList.indexOf(to.path) !== -1) {
30
     if (whiteList.indexOf(to.path) !== -1) {
31
       next()
31
       next()
32
     } else {
32
     } else {
33
-      next('/login')
33
+      next(`/login?redirect=${to.path}`) // 否则全部重定向到登录页
34
       NProgress.done()
34
       NProgress.done()
35
     }
35
     }
36
   }
36
   }

+ 11 - 2
src/views/login/index.vue

@@ -66,7 +66,16 @@ export default {
66
         password: [{ required: true, trigger: 'blur', validator: validatePass }]
66
         password: [{ required: true, trigger: 'blur', validator: validatePass }]
67
       },
67
       },
68
       loading: false,
68
       loading: false,
69
-      pwdType: 'password'
69
+      pwdType: 'password',
70
+      redirect: undefined
71
+    }
72
+  },
73
+  watch: {
74
+    $route: {
75
+      handler: function(route) {
76
+        this.redirect = route.query && route.query.redirect
77
+      },
78
+      immediate: true
70
     }
79
     }
71
   },
80
   },
72
   methods: {
81
   methods: {
@@ -83,7 +92,7 @@ export default {
83
           this.loading = true
92
           this.loading = true
84
           this.$store.dispatch('Login', this.loginForm).then(() => {
93
           this.$store.dispatch('Login', this.loginForm).then(() => {
85
             this.loading = false
94
             this.loading = false
86
-            this.$router.push({ path: '/' })
95
+            this.$router.push({ path: this.redirect || '/' })
87
           }).catch(() => {
96
           }).catch(() => {
88
             this.loading = false
97
             this.loading = false
89
           })
98
           })