ソースを参照

feat: 监听storage事件,更新store里用户信息,显示用户信息的组件从store拿数据。

任一存 3 年 前
コミット
c9fe23e8be
3 ファイル変更42 行追加11 行削除
  1. 31 0
      src/Store/index.js
  2. 9 11
      src/components/userInfo.vue
  3. 2 0
      src/pages/material.js

+ 31 - 0
src/Store/index.js

@@ -7,6 +7,9 @@ Vue.use(Vuex)
 
 
 const store = new Vuex.Store({
 const store = new Vuex.Store({
   state: {
   state: {
+    userAvatar: '',
+    userNickName: '',
+    
     info:'',
     info:'',
     backupInfo:'',
     backupInfo:'',
     showInfo:'',
     showInfo:'',
@@ -28,6 +31,9 @@ const store = new Vuex.Store({
     uploadStatusListVideo: [],
     uploadStatusListVideo: [],
   },
   },
   getters: {
   getters: {
+    userAvatar: state => state.userAvatar,
+    userNickName: state => state.userNickName,
+
     isEditing:state=>state.isEditing,
     isEditing:state=>state.isEditing,
     info:state=>state.info,
     info:state=>state.info,
     showInfo:state=>state.showInfo,
     showInfo:state=>state.showInfo,
@@ -49,6 +55,17 @@ const store = new Vuex.Store({
     uploadStatusListVideo: state => state.uploadStatusListVideo,
     uploadStatusListVideo: state => state.uploadStatusListVideo,
   },
   },
   mutations: {
   mutations: {
+    SetUserAvatar(state, avatar) {
+      if (typeof avatar === 'string') {
+        state.userAvatar = avatar
+      }
+    },
+    SetUserNickName(state, nickName) {
+      if (typeof nickName === 'string') {
+        state.userNickName = nickName
+      }
+    },
+    
     SetTabList(state, list) {
     SetTabList(state, list) {
       state.tablist = list
       state.tablist = list
     },
     },
@@ -96,7 +113,21 @@ const store = new Vuex.Store({
     
     
   },
   },
   actions: {
   actions: {
+    watchUserInfo(context) {
+      function cacheUserInfo() {
+        try {
+          const userInfo = JSON.parse(localStorage.getItem('info'))
+          context.commit('SetUserAvatar', userInfo.head)
+          context.commit('SetUserNickName', userInfo.nickName)
+        } catch (error) {
+          console.log(error)
+        }
+      }
 
 
+      cacheUserInfo()
+      window.removeEventListener('store', cacheUserInfo)
+      window.addEventListener('storage', cacheUserInfo)
+    }
   },
   },
   modules: {
   modules: {
 
 

+ 9 - 11
src/components/userInfo.vue

@@ -1,7 +1,7 @@
 <template>
 <template>
   <div class="user-info">
   <div class="user-info">
-    <img class="avatar" :src="head" alt="" />
-    <div class="nickName">{{nickName}}</div>
+    <img class="avatar" :src="userAvatar" alt="" />
+    <div class="nickName">{{userNickName}}</div>
     <i class="iconfont icon-show_drop-down"></i>
     <i class="iconfont icon-show_drop-down"></i>
     <ul class="menu">
     <ul class="menu">
       <li><span @click="onClickPersonalCenter">个人中心</span></li>
       <li><span @click="onClickPersonalCenter">个人中心</span></li>
@@ -11,30 +11,28 @@
 </template>
 </template>
 
 
 <script>
 <script>
+import { mapGetters } from 'vuex'
 export default {
 export default {
   data() {
   data() {
     return {
     return {
-      nickName: '',
-      head: '',
     }
     }
   },
   },
+  computed: {
+    ...mapGetters([
+      'userAvatar',
+      'userNickName'
+    ]),
+  },
   methods: {
   methods: {
     onClickPersonalCenter() {
     onClickPersonalCenter() {
       window.location.href = '/#/information'
       window.location.href = '/#/information'
     },
     },
     onClickLogout() {
     onClickLogout() {
-      // localStorage.removeItem('address')
-      // localStorage.removeItem('token')
-      // localStorage.removeItem('info')
-      // localStorage.removeItem('currentName')
       localStorage.setItem('token', '')
       localStorage.setItem('token', '')
       window.location.href = '/#/'
       window.location.href = '/#/'
     }
     }
   },
   },
   mounted() {
   mounted() {
-    const userInfo = JSON.parse(localStorage.getItem('info'))
-    this.nickName = userInfo.nickName
-    this.head = userInfo.head
   }
   }
 }
 }
 
 

+ 2 - 0
src/pages/material.js

@@ -24,6 +24,8 @@ Vue.use(Viewer,{
 
 
 Vue.config.productionTip = false
 Vue.config.productionTip = false
 
 
+store.dispatch('watchUserInfo')
+
 new Vue({
 new Vue({
   router,
   router,
   store,
   store,