vuejs / vuex

🗃️ Centralized State Management for Vue.js.
https://vuex.vuejs.org
MIT License
28.42k stars 9.57k forks source link

dynamic register same module, will trigger multi times #1846

Open zenHeart opened 4 years ago

zenHeart commented 4 years ago

Version

3.5.1

Reproduction link

https://codepen.io/zenheart/pen/oNLbvJO

Steps to reproduce

see demo https://codepen.io/zenheart/pen/oNLbvJO

What is expected?

Why registerModule call the same path multi times, will replace old path, but push handle to _mutations, see demo https://codepen.io/zenheart/pen/oNLbvJO

What is actually happening?

this will make add run twice. It's weird, I think we should replace all, this feature is expected, why?

kiaking commented 3 years ago

Oh thanks for the report. Good catch. I think this is not intended, but might be hard to solve...

When registering the same module twice, we can't be sure if it's really the same module. An user might be registering different module with the same name.

Either way, should we call unregisterModule before registering any module if it has the same name...? I think that might be what people wants.

Alanscut commented 3 years ago

when a module with the same name is registered repeatedly, there is an warning on the console: image

And maybe it's a deliberate design, please check the doc: https://vuex.vuejs.org/guide/modules.html#namespacing

By default, actions and mutations are still registered under the global namespace - this allows multiple modules to react to the same action/mutation type. so if you register a module twice, then mutatation will respond twice correspondingly.