Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating a new module via registerModule calling unrelated watches #524

Closed
doonetheprune opened this issue Dec 14, 2016 · 10 comments
Closed
Assignees
Labels
2.x enhancement New feature or request

Comments

@doonetheprune
Copy link

Whenever I create a new module using registerModule an unrelated watcher is fired in the below component. The module created is not related to the existing scope module in anyway.

Vue.component('component-test', {
    template: _template,
    props: {
        propA: {}
    },
    data: function() {
        return {
            data: 'a'
        };
    },
    watch: {
        'scope.contacts.selected': function(newVal, oldVal) {
            console.log('I am called but why???');
        }
    },
    computed: {
        scope: function() {
            return this.$store.state.scope;
        }
    }
});
@ktsn
Copy link
Member

ktsn commented Dec 14, 2016

Hi, thanks for reporting this issue.
Can you please provide live reproduction on jsfiddle or codepen etc?

@ktsn ktsn added the need repro Reproduction code is required label Dec 14, 2016
@LinusBorg LinusBorg added the 2.x label Dec 15, 2016
@yyx990803
Copy link
Member

Currently when you register a new module, the store is considered reset. This will trigger all watchers that are watching the store state.

@doonetheprune
Copy link
Author

Is this is something, that will be changed in a future version i.e will there be an option to stop a store reset on register.

@ktsn ktsn removed the need repro Reproduction code is required label Jan 3, 2017
@ktsn
Copy link
Member

ktsn commented Jan 30, 2017

I maybe wrong but I noticed we can avoid this behavior.
We currently reset the state of old inner Vue instance explicitly when a new module is added. I guess we can just omit this process if the reset is caused by registering a new module because the state reset is for hot update.

@ktsn ktsn added the enhancement New feature or request label Jan 30, 2017
@ktsn ktsn self-assigned this Feb 4, 2017
@mojodev
Copy link

mojodev commented Jun 14, 2019

This issue still persist in v3.1.1. i have loaded one module dynamically using registerModule on a page. one of my parent component is watching other module state variable. so while registering module, watcher on unrelated module state is firing. same thing happening for unRegisterModule. @yyx990803 please have a look on this.

@mojodev
Copy link

mojodev commented Jun 17, 2019

Anyone has solution to ignore firing watchers on module registration or unregistration?

@mojodev
Copy link

mojodev commented Jul 1, 2019

any update regarding this issue ?

@ThomasKientz
Copy link

ThomasKientz commented Aug 17, 2019

@mojodev To prevent firing watchers when registering a new module, add a property with the name of the future module you wish to register in the main store with a null value :

// main store
state() {
  return {
    auth : null,
  }
},

then :

Store.registerModule('auth', {...})

@disovi
Copy link

disovi commented Jul 24, 2020

To workaround this issue for dynamically created modules, when module name is known only at runtime - a nested store can be used:

const nestedRoot = 'nestedRoot';
// register nested root
store.registerModule(nestedRoot, {
  namespaced: true,
  state: {},
});
const someDynamicName = 'someId';
// register nested modules, rootStore is not updated
sotre.registerModule([nestedRoot, someDynamicName], {
  // dynamic module data
});

@v1talii-dev
Copy link

To workaround this issue for dynamically created modules, when module name is known only at runtime - a nested store can be used:

const nestedRoot = 'nestedRoot';
// register nested root
store.registerModule(nestedRoot, {
  namespaced: true,
  state: {},
});
const someDynamicName = 'someId';
// register nested modules, rootStore is not updated
sotre.registerModule([nestedRoot, someDynamicName], {
  // dynamic module data
});

Not work on [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.x enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

8 participants