简体   繁体   English

将新项目添加到反应式 object 的数组中会替换数组中的所有项目。 为什么?

[英]Adding new item to an array of a reactive object replaces all items in the array. Why?

The main problem is in the title.主要问题在于标题。 My guess would be that the ref() and reactive() are somehow messing with each other, I just don't know how and what I could do about it.我的猜测是 ref() 和 reactive() 以某种方式相互混淆,我只是不知道如何以及我能做些什么。

The component using the store:使用商店的组件:

<script setup>
    import { ref } from "vue";
    import { store } from "@/store.js";

    const game = ref({
        date: null,
        location: null,
        length: null,
    });
</script>

<template>
    <form @submit.prevent="store.addGame(game)">
        <input v-model="game.date" type="date" required />
        <input v-model="game.location" type="text" placeholder="Location" required />
        <input v-model="game.length" type="number" placeholder="Length (in minutes)" required />
        <button class="button" type="submit">Submit ✔</button>
    </form>
</template>

The problem occurs in the store, in the games array, after I add a second or more items...问题出现在商店中,在游戏数组中,在我添加第二个或更多项目后......

store.js:商店.js:

import { reactive } from "vue";

export const store = reactive({
    games: [],

    addGame(game) {
        this.games.push(game);
        console.log(this.games);
    }
});

The reason is because all the objects that you push inside games share the same reference.原因是您在games中推送的所有对象都共享相同的引用。 To fix this, do this change in store.js要解决此问题,请在store.js中进行此更改

import { reactive } from "vue";

export const store = reactive({
    games: [],

    addGame(game) {
        this.games.push({...game}); // line changed
        console.log(this.games);
    }
});

It's not "replace", all the game reference to the same reactive variable defined in const game = ref() .它不是“替换”,所有game都引用了const game = ref()中定义的同一个反应变量。

Whenever you call store.addGame(game) , you added the same game to the store.每当您调用store.addGame(game)时,您都会将相同game添加到商店中。 Because the game is reactive, is will automatic implement the latest input value by v-model .因为game是反应式的,所以会通过v-model自动实现最新的输入值。

If you put another button to check the value in store.如果您放置另一个按钮来检查存储中的值。 Change the input value, and then click Test button, you can see the store.games changes too.更改输入值,然后单击测试按钮,您可以看到store.games也发生了变化。

<template>
    <form @submit.prevent="store.addGame(game)">
        <input v-model="game.date" type="date" required />
        <input v-model="game.location" type="text" placeholder="Location" required />
        <input v-model="game.length" type="number" placeholder="Length (in minutes)" required />
        <button class="button" type="submit">Submit ✔</button>
    </form>
    <button @click="console.log(store.games)">Test</button>
</template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 数组项不添加但替换当前项? - array item not adding but replaces current item? $emit object 到父组件,并推送到数组。 数组中的对象仍然是反应性的,为什么? - $emit object to parent component, and push to array. Objects in array continue to be reactive, why? 节点-将对象推送到数组将替换第一项 - Node - pushing an object to an array replaces the first item 在JSON数组中查找Item并将其添加到另一个Array中。 Java脚本 - Finding Item in JSON Array and adding it to another Array. Javascript 将 object 中的所有数组合并为一个数组。 - Combine all arrays in object into one array. 在淘汰赛中向对象数组添加新项目 - Adding new item to object array in Knockout 将项目添加到对象中的数组 - Adding Items to An Array In an Object 为什么不是一个包含所有项目的数组,而是每个项目都有一个数组? - Why is there not one array with all the items in it, but an array for every item? 为什么在 MongoDB updateOne 中使用 slice 不会从数组中删除一个项目,而只会用一个奇怪的 object 替换它? - Why does using slice in MongoDB updateOne does not delete an item from an array but only replaces it with a weird object? ReactJS:用新值替换数组中的对象将替换整个数组 - ReactJS: replacing object from array with new value replaces whole array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM