search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

1 answers
Q&A Push objects to array, composable API
Push objects to array, composable API
P粉788765679 2024-04-05 14:41:47 Views 2101

0

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?

Your Answer
submit

1 answers
0

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.

2024-04-06 12:05:10

submit

Popular tool

vc9-vc14 (32+64 bit) runtime library collection (link below)

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

VC9 32-bit phpstudy integrated installation environment runtime library

PHP programmer toolbox full version

PHP programmer toolbox full version

Programmer Toolbox v1.0 PHP Integrated Environment

VC11 32-bit

VC11 32-bit

VC11 32-bit phpstudy integrated installation environment runtime library

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use