data(){
return{
tmp1:{
t:1
},
tmp2:{
t:''
}
}
},
将tmp1的内容赋值给tmp2,改变tmp2里对象的值,发现tmp1里的值也跟着变化了。
为什么会出现这种情况呢?
其实,这是一个引用传递而不是值传递,tmp1和tmp2指向的是同一个内存地址。
如果我们不想让temp1的值跟着联动变化,应该怎么做呢?直接看代码
this.temp2= JSON.parse(JSON.stringify(this.temp1));
this.temp2.t = 'ssss';
console.log( this.temp1.t);//打印结果:1
console.log( this.temp2.t);//打印结果:ssss