在Vue组件中,我有一个名为selectedSuppliers
的(对象)数组。我想将名为suppliers
的数据属性初始化为selectedSuppliers
,但是随后对suppliers
所做的任何更改都应该不传播给selectedSuppliers
。
我尝试了以下
props: {
selectedSuppliers: {
type: Array,
required: true
},
},
data () {
return {
selected: [...this.selectedSuppliers],
}
}
但是它不起作用。将数组数据属性初始化为数组prop的正确方法是什么?
答案 0 :(得分:0)
如果selectedSuppliers
是对象数组,则散布运算符仅执行此数组的浅表副本。这意味着更新suppliers
的任何对象 将更新selectedSuppliers
的内容。
您可以看看this short post。
答案 1 :(得分:0)
这可能有效,
props: {
selectedSuppliers: {
type: Array,
required: true
},
},
data () {
return {
selected: this.selectedSuppliers.map(o => Object.assign({}, o)
}
}