简体   繁体   English

Javascript对象的深层副本在Vue.js中无法正常工作

[英]Deep copy of a Javascript object is not working as expected in Vue.js

I am passing a clone of an object from a parent component to a child component using props , but when I change the value of the status property in the object of the parent component the child component gets notified and chenges the value of the status property in the "cloned" object. 我正在使用props将父组件的对象克隆传递给子组件,但是当我在父组件的对象中更改status属性的值时,子组件会得到通知并更改status属性的值。 “克隆”对象。

I've read about Object.assign() method and that it only does shallow copying but the strange thing is that my object properties are of primitive type String meaning they should be copied by value not by reference, I even tried assigning the values manually and tried the JSON way as demonstrated below but nothing works as I expected. 我已经读过关于Object.assign()方法,并且它只进行浅层复制,但奇怪的是我的对象属性是原始类型String意味着它们应该通过值而不是通过引用来复制,我甚至尝试手动分配值并尝试了JSON方式,如下所示,但没有任何工作,如我所料。

Parent Component: AppServers 父组件: AppServers

<template>
  <div>
    <AppServerStatus v-for="server in servers"   :serverObj="JSON.parse(JSON.stringify(server))">
    </AppServerStatus>
    <hr>
    <button @click="changeStatus()">Change server 2</button>
  </div>
</template>

<script>
  import AppServerStatus from './AppServerStatus';

  export default {
    name: "AppServers",
    components: {
      AppServerStatus
    },
    data() {
      return {
        servers: [
          {
            name: 'server1',
            status: 'Critical'
          },
          {
            name: 'server2',
            status: 'Normal'
          },
          {
            name: 'server3',
            status: 'abnormal'
          },
          {
            name: 'server4',
            status: 'idle'
          },
          {
            name: 'server5',
            status: 'Good'
          },
        ],
        serverTmp: {}
      }
    },
    methods: {
      changeStatus(){
        this.servers[1].status = 'Active';
      }
    }
  }
</script>

Child Component: AppServerStatus 子组件: AppServerStatus

<template>
  <div>
    <h3>Server Name: {{ serverObj.name }}</h3>
    <p>Server Status: {{ serverObj.status }}</p>
    <hr>
  </div>
</template>

<script>
  export default {
    name: "AppServerStatus",
    data() {
      return {
        status: 'Critical'
      }
    },
    props: [
      'serverObj'
    ]
  }
</script>

I expect the value of status property in the object of the child component to stay Normal when I execute changeStatus() in the parent component but it changes also. 我期望当我在父组件中执行changeStatus()时,子组件的对象中的status属性的值保持正常 ,但它也会更改。

Create a new object from the serverObj prop on created or mounted to prevent unwanted reactivity. serverObj prop createdmounted新对象以防止不必要的反应。

<template>
  <div>
    <h3>Server Name: {{ server.name }}</h3>
    <p>Server Status: {{ server.status }}</p>
    <hr>
  </div>
</template>

<script>
  export default {
    name: 'AppServerStatus',
    data() {
      return {
        status: 'Critical',
        server: {
          name: '',
          status: ''
        }
      }
    },

    props: [
      'serverObj'
    ],

    mounted() {
       this.server = {
         name: this.serverObj.name,
         status: this.serverObj.status
       };
    }
  }
</script>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM