1

I want to get date form getters in router. I have 2 modules in my store:

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

  return Store
}

And this is my auth/index.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
}

I want to check if my user is authenthcated so before go to component I want to check it.

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

Now in router/index.js I declare my function

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

How can I call my getters to return value or how can I call my store for auth module?

Pawel
  • 427
  • 2
  • 7
  • 15

2 Answers2

2

That would be:

this.$store.getters['auth/isAuthenticated']
danb4r
  • 322
  • 2
  • 9
0

There are a few possibilities I would suggest trying. First of all, take a look at this answer (How to access the getter from another vuex module?).

I would try:

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

And then try adding RootGetters and test the same pieces again. If that doesn’t work you can try namespacing (What exactly is namespacing of modules in vuex)

store.getters[‘auth/isAuthenticated’]

Or you can try setting up an enum with your authGetters in your store

export enum authGetters {
  IS_AUTHENTICATED = 'isAuthenticated'
}

store.getters[authGetters.IS_AUTHENTICATED]