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
When using strict mode, you want to provide a deep copy of 'state' and 'getter' methods #1758
Comments
Sorry what is 'dep'? I've stored almost all kind of crazy data structure to state but never encountered the error. Could you provide the reproduction code for this? |
Since @kiaking |
这是一个 'codesandbox' 例子,请查看,Take a look at the console. While it doesn't affect the production environment, it does affect the developer's judgment in the development environment |
You're not deep coping the object. So the warning is correct, you are mutating the original state inside component. At first your store mutation is wrong. You need to change it to: mutations: {
changeTest(state, list) {
// state.test = list
state.testList = list
}
} Then, change your changeList () {
let list = this.testList.slice()
list = list.map(v => {
// Copy object.
const newItem = { ...v }
newItem.foo = 'barnew'
return newItem
})
this.changeTest(list)
} However, if this is all you want to do, you can just map the list and be done with it. changeList () {
const list = this.testList.map((v) => {
return { foo: 'barnew' }
})
this.changeTest(list)
} Hope it gets you where you want to go 👍 |
What problem does this feature solve?
In strict mode, when you make a deep copy of an object inside 'state', the 'dep' in the object will be copied as well, and an error will be reported: 'do not mutate vuex store state outside mutation handlers.'
What does the proposed API look like?
function deepCopy (name) { return JSON.parse(JSON.stringify(state[name])) }
The text was updated successfully, but these errors were encountered: