There is a project. There are two selection options "size" (large, small) and quantity (2/4/6).
When each option is selected, the selection needs to be displayed in the title (assuming a small sum of 4). After clicking the "Add to Storage" button, it will be added to the storage and a list will be displayed on another page.
What I tried:
vue ts:
const chosenMachines = reactive([]);
const machineInfo = ref({
size: "Standart",
glasses: 6
})
const addToList = () => {
// myStore.addToStoredItems(machineInfo) ---> storage pinia
chosenMachines.push(machineInfo.value)
}
template:
Title {{ machineInfo.size }} | {{ machineInfo.glasses }}
When adding the first object and the second object thereafter, the first object is overwritten. What is the reason?
1 answers
Push machineInfo.value into the array retaining a reference to the original object. You just made a shallow copy. Any updates to machineInfo.value will cause the values in the array to also be updated. You need to make a deep copy before pushing.
chosenMachines.push({...machineInfo.value})
There are many other ways to do a deep copy, and depending on your data, there may be one method that is more suitable.
Hot tools Tags
Hot Questions
Popular tool
vc9-vc14 (32+64 bit) runtime library collection (link below)
Download the collection of runtime libraries required for phpStudy installation
VC9 32-bit
VC9 32-bit phpstudy integrated installation environment runtime library
PHP programmer toolbox full version
Programmer Toolbox v1.0 PHP Integrated Environment
VC11 32-bit
VC11 32-bit phpstudy integrated installation environment runtime library
SublimeText3 Chinese version
Chinese version, very easy to use
Hot Topics
20414
7
13573
4






