Enable "state" as a valid key in getters #452
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Issue
When I declare a
state
key in getters as follow, there is an error:Check out the simple demo and the error will be logged in the console.
Debug
Finally I found there will be a circular call if declaring
state
key in getters:Getters
is treated as computed properties andstate
as data properties (under astate
key) inVuex.Store._vm
. However, in vue, if there is a same key both incomputed
anddata
, we will always get the computed property using proxied properties of the vue instance.When getting store state, the
state
in getters is returned instead of the single state tree based on step 1.Invoke the
state
getter function which will accessstore.state
at first. So it jumps to step 2 and leads to a endless call stack.Fix
Access the store state through instance properties (prefixed with
$
) instead of proxied properties in order to avoid overlapping between data and computed properties in vue instance.