Listen to actions/mutations from within a vuex modules
I have a vuex store with several modules, and I want to be able to listen to "global" dispatch
or commit
calls (aka actions and mutations) from within a module. For example, in an auth
module there are actions like login and logout. Whenever those actions are dispatched, I want another module ui
to also perform some action, like setting a language based on the logged in user.
I know I can just dispatch the ui
actions from within the login and logout actions in the auth
module. But I'd rather have it the other way around, so that ui
is listening to the events (actions and/or mutations) dispatched in auth
. This way I can easily create side effects without changing the "original" vuex module.
Maybe I need to use watches, but some actions don't alter state so I can't always use them. Creating a plugin is also an option, but I don't want to create an extra plugin each time I want to create such a side effect.
So my question; from within a vuex module, how can I listen to actions/mutations dispatched from other modules?
javascript vue.js vuex vuex-modules
add a comment |
I have a vuex store with several modules, and I want to be able to listen to "global" dispatch
or commit
calls (aka actions and mutations) from within a module. For example, in an auth
module there are actions like login and logout. Whenever those actions are dispatched, I want another module ui
to also perform some action, like setting a language based on the logged in user.
I know I can just dispatch the ui
actions from within the login and logout actions in the auth
module. But I'd rather have it the other way around, so that ui
is listening to the events (actions and/or mutations) dispatched in auth
. This way I can easily create side effects without changing the "original" vuex module.
Maybe I need to use watches, but some actions don't alter state so I can't always use them. Creating a plugin is also an option, but I don't want to create an extra plugin each time I want to create such a side effect.
So my question; from within a vuex module, how can I listen to actions/mutations dispatched from other modules?
javascript vue.js vuex vuex-modules
1
Possible duplicate of Vue listen for Vuex commit?
– Phiter
Nov 12 '18 at 19:55
Maybe you can subscribe from within the module
– Phiter
Nov 12 '18 at 19:56
thanks haven't found that one yet, but it suggests using a plugin. And I'm wondering if there is another way to listen to (or subscribe) to the actions in a module
– Wessel van der Linden
Nov 12 '18 at 19:56
I think a plugin would solve it for you
– Phiter
Nov 12 '18 at 19:57
Using it as a plugin is optional. Subscribe API can be registered on the store instance after it has been created. It is not possible to listen from within a module, since modules are compiled into a single store instance.
– aBiscuit
Nov 12 '18 at 20:01
add a comment |
I have a vuex store with several modules, and I want to be able to listen to "global" dispatch
or commit
calls (aka actions and mutations) from within a module. For example, in an auth
module there are actions like login and logout. Whenever those actions are dispatched, I want another module ui
to also perform some action, like setting a language based on the logged in user.
I know I can just dispatch the ui
actions from within the login and logout actions in the auth
module. But I'd rather have it the other way around, so that ui
is listening to the events (actions and/or mutations) dispatched in auth
. This way I can easily create side effects without changing the "original" vuex module.
Maybe I need to use watches, but some actions don't alter state so I can't always use them. Creating a plugin is also an option, but I don't want to create an extra plugin each time I want to create such a side effect.
So my question; from within a vuex module, how can I listen to actions/mutations dispatched from other modules?
javascript vue.js vuex vuex-modules
I have a vuex store with several modules, and I want to be able to listen to "global" dispatch
or commit
calls (aka actions and mutations) from within a module. For example, in an auth
module there are actions like login and logout. Whenever those actions are dispatched, I want another module ui
to also perform some action, like setting a language based on the logged in user.
I know I can just dispatch the ui
actions from within the login and logout actions in the auth
module. But I'd rather have it the other way around, so that ui
is listening to the events (actions and/or mutations) dispatched in auth
. This way I can easily create side effects without changing the "original" vuex module.
Maybe I need to use watches, but some actions don't alter state so I can't always use them. Creating a plugin is also an option, but I don't want to create an extra plugin each time I want to create such a side effect.
So my question; from within a vuex module, how can I listen to actions/mutations dispatched from other modules?
javascript vue.js vuex vuex-modules
javascript vue.js vuex vuex-modules
asked Nov 12 '18 at 19:53
Wessel van der LindenWessel van der Linden
1,27511125
1,27511125
1
Possible duplicate of Vue listen for Vuex commit?
– Phiter
Nov 12 '18 at 19:55
Maybe you can subscribe from within the module
– Phiter
Nov 12 '18 at 19:56
thanks haven't found that one yet, but it suggests using a plugin. And I'm wondering if there is another way to listen to (or subscribe) to the actions in a module
– Wessel van der Linden
Nov 12 '18 at 19:56
I think a plugin would solve it for you
– Phiter
Nov 12 '18 at 19:57
Using it as a plugin is optional. Subscribe API can be registered on the store instance after it has been created. It is not possible to listen from within a module, since modules are compiled into a single store instance.
– aBiscuit
Nov 12 '18 at 20:01
add a comment |
1
Possible duplicate of Vue listen for Vuex commit?
– Phiter
Nov 12 '18 at 19:55
Maybe you can subscribe from within the module
– Phiter
Nov 12 '18 at 19:56
thanks haven't found that one yet, but it suggests using a plugin. And I'm wondering if there is another way to listen to (or subscribe) to the actions in a module
– Wessel van der Linden
Nov 12 '18 at 19:56
I think a plugin would solve it for you
– Phiter
Nov 12 '18 at 19:57
Using it as a plugin is optional. Subscribe API can be registered on the store instance after it has been created. It is not possible to listen from within a module, since modules are compiled into a single store instance.
– aBiscuit
Nov 12 '18 at 20:01
1
1
Possible duplicate of Vue listen for Vuex commit?
– Phiter
Nov 12 '18 at 19:55
Possible duplicate of Vue listen for Vuex commit?
– Phiter
Nov 12 '18 at 19:55
Maybe you can subscribe from within the module
– Phiter
Nov 12 '18 at 19:56
Maybe you can subscribe from within the module
– Phiter
Nov 12 '18 at 19:56
thanks haven't found that one yet, but it suggests using a plugin. And I'm wondering if there is another way to listen to (or subscribe) to the actions in a module
– Wessel van der Linden
Nov 12 '18 at 19:56
thanks haven't found that one yet, but it suggests using a plugin. And I'm wondering if there is another way to listen to (or subscribe) to the actions in a module
– Wessel van der Linden
Nov 12 '18 at 19:56
I think a plugin would solve it for you
– Phiter
Nov 12 '18 at 19:57
I think a plugin would solve it for you
– Phiter
Nov 12 '18 at 19:57
Using it as a plugin is optional. Subscribe API can be registered on the store instance after it has been created. It is not possible to listen from within a module, since modules are compiled into a single store instance.
– aBiscuit
Nov 12 '18 at 20:01
Using it as a plugin is optional. Subscribe API can be registered on the store instance after it has been created. It is not possible to listen from within a module, since modules are compiled into a single store instance.
– aBiscuit
Nov 12 '18 at 20:01
add a comment |
1 Answer
1
active
oldest
votes
I created a vuex plugin for myself which can will trigger all refresh action in all stores. But you can do anything else where I called the refresh.
Plugin:
const onModuleAValueChange= (store) => {
store.watch(
state => state.moduleA.value,
(val, oldVal) => {
// Don't do anything on init state
if (!oldVal) return;
// This will trigger all refresh actions on all store. But you can add anything here
// Essentially does:
// store.dispatch(`moduleA/refresh`);
// store.dispatch(`moduleB/refresh`);
// store.dispatch(`moduleC/refresh`);
for (let state in store.state) {
const action = `${state}/refresh`;
// If the store does not have an refresh action ignore
if (store._actions[action]) store.dispatch(`${state}/refresh`);
}
// Additional action
store.dispatch(`moduleC/moduleC_Action`);
}
);
};
Store core:
export const store = new Vuex.Store({
modules: {
moduleA,
moduleB,
moduleC
},
plugins: [onModuleAValueChange]
});
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269187%2flisten-to-actions-mutations-from-within-a-vuex-modules%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I created a vuex plugin for myself which can will trigger all refresh action in all stores. But you can do anything else where I called the refresh.
Plugin:
const onModuleAValueChange= (store) => {
store.watch(
state => state.moduleA.value,
(val, oldVal) => {
// Don't do anything on init state
if (!oldVal) return;
// This will trigger all refresh actions on all store. But you can add anything here
// Essentially does:
// store.dispatch(`moduleA/refresh`);
// store.dispatch(`moduleB/refresh`);
// store.dispatch(`moduleC/refresh`);
for (let state in store.state) {
const action = `${state}/refresh`;
// If the store does not have an refresh action ignore
if (store._actions[action]) store.dispatch(`${state}/refresh`);
}
// Additional action
store.dispatch(`moduleC/moduleC_Action`);
}
);
};
Store core:
export const store = new Vuex.Store({
modules: {
moduleA,
moduleB,
moduleC
},
plugins: [onModuleAValueChange]
});
add a comment |
I created a vuex plugin for myself which can will trigger all refresh action in all stores. But you can do anything else where I called the refresh.
Plugin:
const onModuleAValueChange= (store) => {
store.watch(
state => state.moduleA.value,
(val, oldVal) => {
// Don't do anything on init state
if (!oldVal) return;
// This will trigger all refresh actions on all store. But you can add anything here
// Essentially does:
// store.dispatch(`moduleA/refresh`);
// store.dispatch(`moduleB/refresh`);
// store.dispatch(`moduleC/refresh`);
for (let state in store.state) {
const action = `${state}/refresh`;
// If the store does not have an refresh action ignore
if (store._actions[action]) store.dispatch(`${state}/refresh`);
}
// Additional action
store.dispatch(`moduleC/moduleC_Action`);
}
);
};
Store core:
export const store = new Vuex.Store({
modules: {
moduleA,
moduleB,
moduleC
},
plugins: [onModuleAValueChange]
});
add a comment |
I created a vuex plugin for myself which can will trigger all refresh action in all stores. But you can do anything else where I called the refresh.
Plugin:
const onModuleAValueChange= (store) => {
store.watch(
state => state.moduleA.value,
(val, oldVal) => {
// Don't do anything on init state
if (!oldVal) return;
// This will trigger all refresh actions on all store. But you can add anything here
// Essentially does:
// store.dispatch(`moduleA/refresh`);
// store.dispatch(`moduleB/refresh`);
// store.dispatch(`moduleC/refresh`);
for (let state in store.state) {
const action = `${state}/refresh`;
// If the store does not have an refresh action ignore
if (store._actions[action]) store.dispatch(`${state}/refresh`);
}
// Additional action
store.dispatch(`moduleC/moduleC_Action`);
}
);
};
Store core:
export const store = new Vuex.Store({
modules: {
moduleA,
moduleB,
moduleC
},
plugins: [onModuleAValueChange]
});
I created a vuex plugin for myself which can will trigger all refresh action in all stores. But you can do anything else where I called the refresh.
Plugin:
const onModuleAValueChange= (store) => {
store.watch(
state => state.moduleA.value,
(val, oldVal) => {
// Don't do anything on init state
if (!oldVal) return;
// This will trigger all refresh actions on all store. But you can add anything here
// Essentially does:
// store.dispatch(`moduleA/refresh`);
// store.dispatch(`moduleB/refresh`);
// store.dispatch(`moduleC/refresh`);
for (let state in store.state) {
const action = `${state}/refresh`;
// If the store does not have an refresh action ignore
if (store._actions[action]) store.dispatch(`${state}/refresh`);
}
// Additional action
store.dispatch(`moduleC/moduleC_Action`);
}
);
};
Store core:
export const store = new Vuex.Store({
modules: {
moduleA,
moduleB,
moduleC
},
plugins: [onModuleAValueChange]
});
edited Nov 13 '18 at 10:22
answered Nov 13 '18 at 10:17
Jeremy WaltersJeremy Walters
49418
49418
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269187%2flisten-to-actions-mutations-from-within-a-vuex-modules%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Possible duplicate of Vue listen for Vuex commit?
– Phiter
Nov 12 '18 at 19:55
Maybe you can subscribe from within the module
– Phiter
Nov 12 '18 at 19:56
thanks haven't found that one yet, but it suggests using a plugin. And I'm wondering if there is another way to listen to (or subscribe) to the actions in a module
– Wessel van der Linden
Nov 12 '18 at 19:56
I think a plugin would solve it for you
– Phiter
Nov 12 '18 at 19:57
Using it as a plugin is optional. Subscribe API can be registered on the store instance after it has been created. It is not possible to listen from within a module, since modules are compiled into a single store instance.
– aBiscuit
Nov 12 '18 at 20:01