Selaa lähdekoodia

feature: add a redirect url when not logged in

https://github.com/PanJiaChen/vue-element-admin/issues/438
Pan 6 vuotta sitten
vanhempi
commit
ccd07c3d63
2 muutettua tiedostoa jossa 12 lisäystä ja 3 poistoa
  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 30
     if (whiteList.indexOf(to.path) !== -1) {
31 31
       next()
32 32
     } else {
33
-      next('/login')
33
+      next(`/login?redirect=${to.path}`) // 否则全部重定向到登录页
34 34
       NProgress.done()
35 35
     }
36 36
   }

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

@@ -66,7 +66,16 @@ export default {
66 66
         password: [{ required: true, trigger: 'blur', validator: validatePass }]
67 67
       },
68 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 81
   methods: {
@@ -83,7 +92,7 @@ export default {
83 92
           this.loading = true
84 93
           this.$store.dispatch('Login', this.loginForm).then(() => {
85 94
             this.loading = false
86
-            this.$router.push({ path: '/' })
95
+            this.$router.push({ path: this.redirect || '/' })
87 96
           }).catch(() => {
88 97
             this.loading = false
89 98
           })