简体   繁体   English

如何从 vue 应用程序中的 main.js 文件访问存储 getter 更新值?

[英]How to access store getter updated value from main.js file in vue app?

I am trying to access updated vuex getter from main.js file to check whether user is logged in or not.我正在尝试从main.js文件访问更新的 vuex getter 以检查用户是否已登录。 Based on that i take user to login page.基于此,我将用户带到登录页面。 In the main.js file i simply accessing store getter like this.在 main.js 文件中,我只是像这样访问存储 getter。

var router = new VueRouter({...})

router.beforeEach((to, from, next) => {
    console.log(store.getters['general/isUserLoggedIn'])
    next()
})

general is the module name. general是模块名称。

but when simply log store it shows object with updated value.但是当简单地记录store时,它会显示 object 的更新值。 but when logging store.getters['general/isUserLoggedIn'] it shows initial state value.但是当记录store.getters['general/isUserLoggedIn']它显示初始 state 值。 why it is not providing updated value.为什么它不提供更新的价值。 Is it right way of doing or any alternative way is there.这是正确的做法还是有任何替代方法。

More details更多细节

in store.js filestore.js文件中

import Vue from 'vue'
import Vuex from 'vuex'
import general from './modules/general'
import other from './modules/other'

Vue.use(Vuex)

export const store = new Vuex.Store({
    modules: {
        general,
        other
    },
    plugins: [],
    strict: process.env.NODE_ENV !== 'production'
})

checking user logged in or not by calling api status In App.vue file通过在App.vue文件中调用 api 状态检查用户是否登录

axios.get('/api/profile').then((response) => {
    if (response.data.status === 'success') {
        this.setUserLoggedIn(true)
    }
})

actions.js in general module通用模块中的actions.js

setUserLoggedIn ({ commit }, data) {
    commit(types.SET_USER_LOGGED_IN, data)
}

mutations.js in general module通用模块中的mutation.js

const mutations = {
    [types.SET_USER_LOGGED_IN](state, data) {
        state.isUserLoggedIn = data
    }
}

On the initial navigation, the login isn't complete yet in the guard, which is why you don't see the value in the console.在初始导航中,防护中的登录尚未完成,这就是您在控制台中看不到该值的原因。 It only appears true when logging store because it's set shortly after the log, and the console updates itself when you log objects/arrays and then click to view their properties.它仅在记录store时才出现,因为它是在记录后不久设置true ,并且当您记录对象/数组然后单击以查看它们的属性时,控制台会自行更新。

One way to fix the race condition is to redirect to the home page when the login is complete:解决竞争条件的一种方法是在登录完成时重定向到主页:

axios.get('/api/profile').then((response) => {
  if (response.data.status === 'success') {
    this.setUserLoggedIn(true)
    this.$router.push('/');  // Route to home page once login is complete
  }
})

And before that, redirect to login if the user is not logged in:在此之前,如果用户未登录,则重定向到登录:

router.beforeEach((to, from, next) => {
  const isLoggedIn = store.getters['general/isUserLoggedIn'];
  if(!isLoggedIn) {
    return next('/login');
  }
  next()
})
  • On the initial load, the user will be redirected to the login page.在初始加载时,用户将被重定向到登录页面。
  • Once the login completes, they will be sent back to the home page.登录完成后,它们将被发送回主页。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM