Open ashahbazFV opened 4 years ago
I also had the problem because of this. And note that is also happens with other plugins similar to vuex-persist
, such as vuex-persistedstate
(robinvdvleuten/vuex-persistedstate#311).
I agree with your proposed solution, but I think there should also be a way to merge default state with stored state, so all keys from stored state will stay the same while adding all other keys/fields from the default store. This will be useful in cases when new keys are added to the store, but they don't exist yet in localStorage. If merging is implemented, the new keys would just be merged with existing ones. If not, complete localStorage module would overwrite new keys, causing them to be undefined.
Version
3.1.2
Reproduction link
https://codesandbox.io/s/compassionate-hermann-msyze
Steps to reproduce
dynamic
andpreserveState
flags totrue
getModule
function to initialise the module from vuex-module-decoratorssomeProperty
will beundefined
What is expected?
When installing a dynamic module and there is no state present, a default state should be created
What is actually happening?
When installing a dynamic module and there is no state present, the module is not registered causing reads to the state for that module to fail.
NOTE: Was unable to reproduce the issue cleanly in CodeSandbox so the reproduction is not working, but the key aspects are still there.
Upon investigation, it was discovered the vuex-module-decorators lib passes their
preserveState
flag down to the vuex layer. When the dynamic module is installed with theinstallModule
function and thehot
option is enabled, the current implementation skips creating any state for the new module. This introduces side effects, where the module is registered in the vuex store but the state can't be retrieved if there was no state previously present.The vuex-persist lib is in use, but it is used to hydrate any past state down to vuex. The store, while using the vuex-persist lib, has the data initialised in the following order.
Upon modifying the
installModule
function, to check if the state of the new dynamic module exists, the problem is resolved. Essentially, if the state of the module doesn't currently exist in theparentState
within the store, then the default module state is created and registered.One set of changes that fix this case are as follows.
In
src/store.js
the following code seems to be the offending code.When modified, the default state is created when the
moduleName
isn't found in theparentState
.There may be other ways to fix this issue, this is one potential solution that was discovered after some investigation.