Explorar el Código

Vue 获取本地 cookie 检测用户是否登录

reghao hace 2 años
padre
commit
382220be4d
Se han modificado 1 ficheros con 10 adiciones y 5 borrados
  1. 10 5
      src/components/layout/NavBar.vue

+ 10 - 5
src/components/layout/NavBar.vue

@@ -164,9 +164,11 @@
 </template>
 
 <script>
+import Vue from 'vue'
 import SearchBox from '@/components/layout/SearchBox'
 import { userMixin } from 'assets/js/mixin'
 import { getMyInfo } from '@/api/user'
+import VueCookies from "vue-cookies";
 
 export default {
   name: 'NavBar',
@@ -179,11 +181,14 @@ export default {
     }
   },
   created() {
-    getMyInfo().then(res => {
-      if (res.code === 0) {
-        this.user = res.data
-      }
-    })
+    const userdata = Vue.$cookies.get('USERDATA')
+    if (userdata !== null) {
+      getMyInfo().then(res => {
+        if (res.code === 0) {
+          this.user = res.data
+        }
+      })
+    }
   },
   methods: {
     login() {