Search code examples
javascriptvue.jsvuexvue-storefront

How to access vue instance in Vuex


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.


Solution

  • 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);
        }
      }
    });