无法从路由器的获取器获取数据。[Vuex, quasar]

问题描述 投票:0回答:1

我想在路由器中获取日期表单获取器。我的商店里有2个模块。

export default function (/* { ssrContext } */) {
const Store = new Vuex.Store({
 modules: {
   customers,
   auth
 },
 namespaced: true,
 strict: process.env.DEV
})

  return Store
}

这是我的authindex.js。

import state from "./state";
import * as getters from "./getters"
import * as mutations from "./mutations"
import * as actions from "./actions"

export default {
 namespaced: true,
 state,
 mutations,
 actions,
 getters
}

我想检查我的用户是否被认证,所以在进入组件之前我想检查它。

  {
    path: '',
    component: () => import('pages/Index.vue'),
    beforeEnter: ifAuthenticated()
  },

现在,在routerindex.js中,我声明了我的函数。

export function ifAuthenticated(to, from, next){
console.log(store.auth); //undefined
if (store.auth.getters.isAuthenticated) {
  next();
  return;
}
next("/login");
};

如何调用我的getters来返回值,或者如何调用我的store来进行auth模块?

vue.js vuex vue-router quasar-framework quasar
1个回答
0
投票

有几种可能,我建议你试试。首先,看看这个答案(如何从另一个vuex模块访问getter?).

我会尝试。

this.$store.getters.isAuthenticated()
store.getters.isAuthenticated()
// don’t forget the ()

然后尝试添加RootGetters,再测试一下相同的片段。 如果不行的话,你可以尝试一下命名间距 (vuex中模块的命名间距到底是什么?)

store.getters[‘auth/isAuthenticated’]

或者你可以尝试在你的商店中为你的authGetters设置一个枚举。

export enum authGetters {
  IS_AUTHENTICATED = 'isAuthenticated'
}

store.getters[authGetters.IS_AUTHENTICATED]
© www.soinside.com 2019 - 2024. All rights reserved.