Listen to actions/mutations from within a vuex modules












0















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?










share|improve this question


















  • 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
















0















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?










share|improve this question


















  • 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














0












0








0








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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














  • 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












1 Answer
1






active

oldest

votes


















0














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





share|improve this answer

























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


    }
    });














    draft saved

    draft discarded


















    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









    0














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





    share|improve this answer






























      0














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





      share|improve this answer




























        0












        0








        0







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





        share|improve this answer















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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 10:22

























        answered Nov 13 '18 at 10:17









        Jeremy WaltersJeremy Walters

        49418




        49418






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Full-time equivalent

            さくらももこ

            13 indicted, 8 arrested in Calif. drug cartel investigation