Answer the question
In order to leave comments, you need to log in
How to deal with reactivity (with passing by reference and object deep copying)?
There is some constant block:
const block = {
title: '',
hash: '',
end: {
is_end: 0,
type: 0
}
}
import empty_block from '../../data/empty_block';
let block = {...empty_block}; // пытаюсь избавиться от реактивности
block.title = this.title;
block.hash = Math.floor(Date.now() / 1000);
this.$store.commit('addBlock', block);
const block = {
title: '',
hash: '',
is_end: 0,
type: 0
}
Answer the question
In order to leave comments, you need to log in
The easiest option is to do it the way the Vue developers recommend.
In the empty_block module, instead of exporting an object, you can export a function that returns an object.
export default function () {
return {
title: '',
hash: '',
end: {
isEnd: 0,
type: 0
}
}
}
import emptyBlock from 'emptyBlock'
let block = emptyBlock()
{ ...someObject }
, the objects that it contained someObject
remain the same and with any mutation of all owners of the object reference he is changing. //let block = {...empty_block};
let block = _.cloneDeep(empty_block);
You can try like this:
let block = JSON.parse(JSON.stringify(empty_block))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question