How to access vue instance in Vuex

Rohit Nishad picture Rohit Nishad · Mar 2, 2020 · Viewed 14.1k times · Source

I declare a global variable in the main.js of the Vue.js project.

Vue.prototype.$API = "myapihere"

And I want to use this from everywhere. and it's work properly by using this.$API.

But in Vuex it does not work.

console.log(this.$API);

Here this.$API is undefined.

How I use my $API in Vuex.

Answer

Pierre Said picture Pierre Said · Mar 2, 2020

Vue 2 and Vuex 3 answer

In the store you can access the vue instance by accessing this._vm

const store = new Vuex.Store({
  mutations: {
    test(state) {
      console.log(this._vm);
    }
  }
});