vue 使用this.$set添加更改新属性的问题

在使用到this.$set 给某个对象的属性值赋值的时候,大多格式为this.$set( target, key, value )。

但若value为该对象的原属性数组,如:

 this_.getProductListTable.productList.forEach((item, i) => {
                this_.$set(item, 'index', i);
                this_.$set(item, 'newtags', item.product.tag);
               })
               
//product.tag里面的值为数组

那么之后更改该对象的新属性值(newtags.push(xxxxxx))时,原数组(item.product.tag)会随之改变。

原因可能为this方法中指向的是同一块数据域,

解决方法:1.如果改为value为空再重新进行赋值处理,如

this_.$set(item, 'newMetas', []);

2.或者将value转换为json格式在转出,如:

this_.$set(item, 'newTags', JSON.parse(JSON.stringify(item.product.tag)));

猜你喜欢

转载自blog.csdn.net/weixin_42574985/article/details/128102340
今日推荐