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
this.vue.$notify()
@ankitsinghaniyaz You may want to read more about "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);
},
};
@1isten . You can simply use this on NuxtJS Vuex
I found
this._vmworks
@gaby64 it cannot work because of this instruction, which is called unconditionally when you create an instance of Store
I found
this._vmworks@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
Most helpful comment
this._vm