简体   繁体   English

在 Vue3 Typscript 中将道具传递给数据 object

[英]Passing a prop to data object in Vue3 Typscript

I have a Vue component (a table) which receives an array of objects via props.我有一个 Vue 组件(一个表),它通过道具接收一组对象。 Now I want to delete, add or edit some entries in this array but I cant do that as the prop is ready-only.现在我想删除、添加或编辑这个数组中的一些条目,但我不能这样做,因为道具是现成的。 So I thought upon receiving the property I just copy that thing into an object in the data-section of said component, which i should be able to edit but I havent found out how to do it.所以我想在收到属性后,我只是将那个东西复制到所述组件的数据部分中的 object 中,我应该能够编辑它,但我还没有找到如何去做。

Say items is my property and ditems is my data objectitems是我的财产,而ditems是我的数据 object

  data() {
    return { ditems: this.items };
  },

isnt working and neither is没有工作,也没有

  data() {
    let pitems = Object.assign({}, this.items);
    return { ditems };
  },

so I'm running out of ideas and I hope someone can help me Thanks in advance for your time所以我的想法已经不多了,我希望有人可以帮助我提前感谢您的时间

When you try to edit the props value, you are right about duplicate the prop by either using Object.assign() or JSON.parse(JSON.stringify()) (I recommend the JSON parse way btw).当您尝试编辑道具值时,您可以使用Object.assign()JSON.parse(JSON.stringify())来复制道具(我推荐 Z0ECD11C1D7A287401D148A 方式 btwBB3.7401D148A)

However, the duplicate method is better put within watch instead of data() , given the prop data might be empty initially, and comes with data after an api request or something.但是,复制方法最好放在watch而不是data()中,因为道具数据最初可能是空的,并且在 api 请求或其他东西之后附带数据。 So you'd better do this:所以你最好这样做:

data() {
  return {
    ditems: {}
  }
}
watch: {
  items: {
    handler(val) {
      if (Object.keys(val).length) { // check whether items data is empty
        this.ditems = JSON.parse(JSON.stringify(val))
      }
    },
    deep: true
  }
}

And by the way, what you are using seems like Vue2, since Vue3 uses setup() instead of data() .顺便说一句,您使用的似乎是 Vue2,因为 Vue3 使用setup()而不是data()

If you are using Vue3 there is a utility provided already for this toRef .如果您使用的是 Vue3,那么已经为此toRef提供了一个实用程序。

import { toRef } from "vue";
export default {
  props: {
    items: Object
  },
  setup(props) {
    return {
      ditems: toRef(props.items)
    };
  }
}

If you are using Vue2 you have to clone the prop and pass it to ditems.如果您使用的是 Vue2,则必须克隆该道具并将其传递给 ditems。 In this example I use a clone method from library lodash:在此示例中,我使用库 lodash 中的克隆方法:

import cloneDeep from "lodash.clonedeep"
export default {
  props: {
    items: Object
  },
  data() {
    return { ditems: cloneDeep(this.items) };
  }
}

You can use different clone methods but beware about the accepted solutions.您可以使用不同的克隆方法,但要注意接受的解决方案。

JSON.parse(JSON.stringify()) JSON.parse(JSON.stringify())

JSON.stringify() will encode values that JSON supports. JSON.stringify() 将对 JSON 支持的值进行编码。 Objects with values that can be objects, arrays, strings, numbers and booleans.具有可以是对象、arrays、字符串、数字和布尔值的值的对象。 So if you have a function, it's not going to be cloned or if you have a Date Object it is going to be a String after cloning.因此,如果您有一个 function,它将不会被克隆,或者如果您有一个 Date Object,它将在克隆后成为一个字符串。

On the other hand另一方面

Object.assign() Object.assign()

is only going to make a shallow copy .只会做一个浅拷贝

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

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