Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vuex accessing namespaced module getters in vue router

I am trying to guard my routes by checking if the user is authenticated, this is the example route:

{
  path: '/intranet',
  component: search,
  meta: { requiresAuth: true },
  props: {
    tax: 'type',
    term: 'intranet-post',
    name: 'Intranet'
  }
},

And I am setting the guard like this:

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {

    let authenticated = this.$store.getters['auth/getAuthenticated'];

    if (!authenticated) {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    } else {
      next()
    }
  } else {
    next()
  }
})

This is the vuex module for auth:

import Vue from "vue";

export default {
  namespaced: true,
  state: {
    authenticated: !!localStorage.getItem("token"),
    token: localStorage.getItem("token")
  },
  mutations: {
    login: function(state, token){
        state.authenticated = true;
        state.token = token;
    },
    logout: function(state){
        state.authenticated = false;
        state.token = '';
    }
  },
  actions: {
    login: function({commit}, token){
      localStorage.setItem('token', token);
      commit('login', token);
    },
    logout: function({commit}){
      localStorage.removeItem("token");
      commit('logout');
    }
  },
  getters: {
    getToken: (state) => state.token,
    getAuthenticated: (state) => state.authenticated,
  }
}

But, when I try to access the auth getter like it is shown in the route guard, I get an error:

Cannot read property 'getters' of undefined

What am I doing wrong, how can I fix this?

like image 362
Leff Avatar asked Oct 24 '17 10:10

Leff


People also ask

How do I access Vuex store getters?

Debug the Store In any case you can call console. log(this. $store) to debug the Store. If you do so you will see the getters are prefixed with the namespace in their name.

How do you access getters in mutations Vuex?

You can access getters in actions, because actions get context as the first argument. like this: actions: { action1: (context, payload) => { console. log(context. getters.

How do you access mutations?

In order to gain access to the Mutations system, you must first complete a secondary quest titled Turn and Face the Strange. After playing through a portion of the Blood and Wine main story, a messenger will deliver a letter to Geralt. After reading the letter, the aforementioned quest will become available.

How do you use getters and setters in Vuex?

The action will commit a mutation to update the state. You can't update state directly, it needs to be handled via a mutation. To access the state, we can use a getter to fetch the current user's name. To have that name update in the Vuex store we then need to use a setter which will dispatch an action.


1 Answers

The error message states that this.$store is undefined when it tries to access this.$store.getters, so the problem seems that the store is not initialized or set up the way you expect it to within the router. Accessing the namespaced getters using .getters['name/getter'] is in itself correct.

Following some tutorials, I have store.js which defines my store, and then I import it in my router.js like this:

import store from './store'

and then access it directly with store instead of this.$store:

let authenticated = store.getters['auth/getAuthenticated'];

I think the problem is that this.$store is automatically added to Vue-Components, but the router is not really a component, and is thus missing the $store-member. Importing the store works around this.

like image 164
cello Avatar answered Oct 09 '22 12:10

cello