-
-
Notifications
You must be signed in to change notification settings - Fork 5k
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
Error: [vuex] Do not mutate vuex store state outside mutation handlers #1917
Comments
Add this code to your export const strict = false Edit from @manniL: This only hides the error but does not resolve the root cause! |
Hi, I have vuex setup as modules and am using mutations like so:
Is there aa reason that this fix is not mentioned in the docs, or is it a bug? Thanks |
@alexchopin would you mind explaining how this is a fix? What is happening underneath the hood? Thanks |
I think we know what the issue is. When the payload mutates the object, it will throw an error. EX: If myModule/myMutation = an empty object { } , and the payload you are sending is an object like " payload = { a: '1', b: '2' } " , then it won't work without setting strict to false or unless you specifically declare the 'myMutation' object has the props 'a' and 'b'. // This is payload being set with " this.$store.commit('myModule/myMutation', Payload) " Won't work:
Will work:
|
I'm probably a bit late to the game here but this happened to me when i had a nested object within my state.
i was just adding what ever properties to areaCreator. doing it once is fine but doing it twice and you're trying to overwrite. I used Object.assign({}, state.areaCreator) to first a make a copy of my object before altering it and setting it in my mutation |
@fredski02 I had the same issue but i think i found a solution. First i tried cloning the object with a spread operator: Using |
I ran into this today when doing the following:
With a mutation method as:
As you can see I am editing the state outside of the mutation method and only using the mutation method to replace the state. The way I fixed this error was to do as it states, move all the mutation code inside the mutation method. Now my code is:
And my mutation method is:
Let us know if that helps |
@r-nicol ... you are the man! thanks for pointing out, what is probably the intended use for passing a payload object and what makes vuex happy |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
I upgrade to the latest version(v1.0.0-rc11),how to handle the error?
The text was updated successfully, but these errors were encountered: