-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
How to access Vue(vm) instance in Vuex store? #1399
Comments
@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 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. |
|
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 I went with another solution, passing the app in a call upon creation of the store, something like:
|
it didn't work for me, mounting after assign the root instance did the trick.
|
I found |
Why not ... 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 |
@gaby64 it cannot work because of this instruction, which is called unconditionally when you create an instance of |
works to access Vue prototype objects |
This works for me:
|
|
|
for those who are using Vuex@3.6.2 and are confused by source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions an example invoking a bootstrap-vue toast component: const store = new Vuex.Store({
state: {
foo: '',
},
mutations: {
setFoo(state, {foo}){
state.foo = foo;
}
},
actions: {
verifyFooWrong: ({commit}, {foo}) => {
if (typeof foo !== 'string'){
this._vm.$bvToast.toast( // <--- `'this' is not defined`
'foo must be of type string',
{
title: 'type error',
autoHideDelay: 5000,
variant: 'danger',
}
);
}
else {
commit('setFoo',{foo});
}
},
verifyFooRight({commit}, {foo}){
if (typeof foo !== 'string'){
this._vm.$bvToast.toast(
'foo must be of type string',
{
title: 'type error',
autoHideDelay: 5000,
variant: 'danger',
}
);
}
else {
commit('setFoo',{foo});
}
}
} sorry if this doesn't run; i typed it out in this input field. |
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 doWhat does the proposed API look like?
this.vue.$notify()
The text was updated successfully, but these errors were encountered: