Vuex: How to access Vue(vm) instance in Vuex store?

Created on 10 Sep 2018  路  15Comments  路  Source: vuejs/vuex

What problem does this feature solve?

I have a $notify method on Vue prototype. I need to use that in my action. Is there a way to do that?

I see that on Vuex we have this.$_vm but not sure if that is the right thing to do

What does the proposed API look like?

this.vue.$notify()

proposal

Most helpful comment

this._vm

All 15 comments

@ankitsinghaniyaz You may want to read more about "Event Bus".

https://alligator.io/vuejs/global-event-bus/

You can pass in a vm instance as an argument, so in your component where you dispatch the action, you can call:

this.$store.dispatch('myAction', { vm: this });

Now your action can access vm on the parameter object and call $notify() on it:

myAction(context, { vm }) {
  vm.$notify();
}

IMO, Vuex should not have reference to Vue instance because it is responsible for data. The approach that @jonaskuske provides looks suitable solution.

this._vm

import App from './App.vue';

const myStore = new Vuex.Store({
    state: {
        ...
    },
    actions: {
        myAction(ctx, data) {
              // here you can use this.$app to access to your vue application
        }
    }
});

const app = new Vue({
    el: '#my-div',
    render: h => h(App),
    store: myStore
});

myStore.$app = app; // <--- !!! this line adds $app to your store object

@m00nk this is undefined for me inside Vuex actions. Note that I use Quasar and have a dedicated file for actions, not sure how it would work in another configuration.

I went with another solution, passing the app in a call upon creation of the store, something like:

const app

export const storeInit = (context, a) => {
  app = a
}

export const otherAction = (context) => {
  // use the event bus or whatever
  app.$root.$emit(...)
}
import App from './App.vue';

const myStore = new Vuex.Store({
    state: {
        ...
    },
    actions: {
        myAction(ctx, data) {
              // here you can use this.$app to access to your vue application
        }
    }
});

const app = new Vue({
    el: '#my-div',
    render: h => h(App),
    store: myStore
});

myStore.$app = app; // <--- !!! this line adds $app to your store object

it didn't work for me, mounting after assign the root instance did the trick.

const app = new Vue({
  router,
  store,
  render: h => h(App)
})
store.$app = app
app.$mount('#app') 

I found this._vm works

Why not
import Vue from 'vue'
...
Vue.set(...)

... Works for me

This works for me:

import Vue from 'vue';

// ...

export const actions = {
  yourAction() {
    console.log(Vue.prototype.$nuxt.$notify);
  },
};

See https://stackoverflow.com/a/58557693/10222165

@1isten . You can simply use this on NuxtJS Vuex

I found this._vm works

@gaby64 it cannot work because of this instruction, which is called unconditionally when you create an instance of Store

I found this._vm works

@gaby64 it cannot work because of this instruction, which is called unconditionally when you create an instance of Store

works to access Vue prototype objects

This works for me:

window.$_app = this

this._vm works for me

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jdittrich picture jdittrich  路  3Comments

gongzza picture gongzza  路  3Comments

niallobrien picture niallobrien  路  3Comments

weepy picture weepy  路  3Comments

ijse picture ijse  路  3Comments