当前位置: 首页 > 知识库问答 >
问题:

vue.js - 如何确保 Vue 在登录成功后立即获取到 localStorage 中的值?

归泽宇
2024-01-24

由于登录成功后马上跳转回主页,导致存localStorage操作与路由跳转几乎同时进行,所以跳转回主页后,localStorage还没有存入完成,获取不到用户信息,只有在刷新一下才能获取到值,如何在登陆成功后获取到呢?

user.js里面将用户信息存储到localStorage中:

const actions = {  getUserInfo({    commit,    dispatch,    state  }) {    axios({      method: 'get',      url: '/gateway/api/userInfo',      headers: {        'admin-gateway-token': state.token      }    }).then(res => {      const {        data,        code,        msg      } = res.data      if (code === 0) {        localStorage.setItem('userInfo', data.username)        commit('SET_USERINFO', data)      } else {        console.log('获取人员信息失败:', res)        if (res.data.code === 2000) {          dispatch('app/logOut', {}, {            root: true          })        }        Message({          type: 'error',          message: '获取人员信息失败:' + msg        })      }    }, res => {      Message({        type: 'error',        message: '获取人员信息失败:' + res      })    })  },

共有3个答案

景元忠
2024-01-24

用 await 等这块逻辑执行完再跳转不行么

詹夕
2024-01-24

你都用 store 了,设置 localStorage 的时候同时也操作修改一下 state 的值不行么……为啥一定要用 ls 绕一下。

窦国源
2024-01-24

你的问题是在登录成功后,你希望立即获取到 localStorage 中的值,但因为同时发生了路由跳转和 localStorage 的存储操作,导致在跳转回主页时 localStorage 还没有存入完成,所以获取不到用户信息。

一种可能的解决方案是使用 Vuex。Vuex 是 Vue.js 的状态管理库,它允许你在全局范围内管理应用的状态。在你的情况下,你可以在 Vuex store 中存储用户信息,并在登录成功后立即更新这个状态。

以下是如何使用 Vuex 来解决你的问题的示例:

首先,在 Vuex store 中添加一个新的模块来存储用户信息:

// store.jsimport Vue from 'vue'import Vuex from 'vuex'Vue.use(Vuex)export default new Vuex.Store({  state: {    userInfo: null  },  mutations: {    SET_USERINFO(state, userInfo) {      state.userInfo = userInfo    }  },  actions: {    getUserInfo({ commit }) {      axios({        method: 'get',        url: '/gateway/api/userInfo',        headers: {          'admin-gateway-token': this.$store.state.token        }      }).then(res => {        const { data, code, msg } = res.data        if (code === 0) {          commit('SET_USERINFO', data)        } else {          console.log('获取人员信息失败:', res)          if (res.data.code === 2000) {            this.$store.dispatch('app/logOut', {}, { root: true })          }          Message({ type: 'error', message: '获取人员信息失败:' + msg })        }      }, res => {        Message({ type: 'error', message: '获取人员信息失败:' + res })      })    }  }})

然后,在你的组件中,你可以通过 this.$store.state.userInfo 来访问用户信息。并且,由于你在 Vuex store 中更新了用户信息,它将在登录成功后立即可用,无论是否发生了路由跳转。例如:

// MyComponent.vueexport default {  mounted() {    if (this.$store.state.userInfo) {      console.log('用户信息:', this.$store.state.userInfo)    } else {      console.log('用户信息尚未获取')    }  }}
 类似资料:
  • vue点击登录,跳转不到首页,获取不到sessionstorage值,刷新页面再点登录就可以 请为一下是为啥,是因为sessionStorage不是实时存的吗 login.vue代码 permission.js 路由守卫代码 vuex index.js代码

  • 我使用硒登录网站 https://launch.stellar.org/#/login。但我无法在新页面中获取任何元素。 这里是代码: 线程“main”org.openqa.selenium.InvalidSelectorException中的异常:给定的选择器btn btn-default stellar-按钮ng-绑定无效或不会导致WebElement。发生以下错误:InvalidSelect

  • 问题内容: 我尝试了这个:当isSessionValid getDetails直接否则else facebook.authorize,然后在onActivityResult中获取getDetails 当我的系统上安装了Facebook应用程序时,此方法工作正常。但是,如果未安装,我将获得一个Web视图以输入facebook凭据,并在logcat中显示登录成功,但是没有调用getDetails块。

  • 我正在尝试用JWT身份验证将我的后端连接到Vue中的登录组件。这是我的Vuex操作: 以及我登录表单上的提交方式: 问题是登录表单没有捕捉到错误(如果有),它总是执行部分并重定向到。当我输入正确的电子邮件和密码时,它会正常工作-状态会根据令牌中接收到的信息进行更新。 我在promise方面做错了什么吗?

  • 我已经在我的应用程序中配置了spring saml和spring security。我给出了不同的url模式来识别请求。如果我在app URL中附加/rest,那么它将创建带有基本身份验证的Spring Security上下文。如果我在应用程序URL中附加/saml,那么它将填充IDP登录页面并重定向到索引。成功登录后返回html。 但我被重定向到登录。再次使用html页面而不是索引。html。在