RuicyWu
RuicyWu

Reputation: 3

An error occurred when i used the 'getters' of the Vuex which said “it is not a function”,why?

I use it according to the official example , but it didn't work.

First, i exported an object which contained the function i wanted.

var getters = {
    getToken: (state) => {   
    if (!state.token) {
        state.token = localStorage.getItem('token')
    }
    return state.token
    }
}
export default getters

Then,i imported it in "store"

const store = new Vuex.Store({
    state,    
    getters,  
    actions,  
    mutations 
})

Here is how it called

created()
    {
    
     var token=this.$store.getters.getToken(this.$store.state);
  
    }

Finally,An error occurred when i call its function enter image description here

I want to know how to handle it.

Upvotes: 0

Views: 246

Answers (1)

B. Fleming
B. Fleming

Reputation: 7230

Unfortunately you haven't provided the code that accesses the getter, so we can't tell for certain what your problem is, but I would wager based on the specific error that you're calling getToken as this.$store.getters.getToken(). This isn't how you access a getter in vuex. Instead, you simply do this.$store.getters.getToken, without the parentheses.

Upvotes: 1

Related Questions