简体   繁体   English

带有道具的 Vue3 初始化变量

[英]Vue3 init variable with props

const props = defineProps({
a: Object
})

function calcs() {
var b = props.a;
b.push({
xxx: yyy
});
}

eg a={x: 'y'}例如 a={x: 'y'}

I need b to be {x: 'y'} before b.push each time when call function calcs, but b keep pushing xxx:yyy, any solutions?每次调用 function 计算时,我都需要在 b.push 之前将 b 设为 {x: 'y'},但 b 一直在推送 xxx:yyy,有什么解决方案吗? I tried ref(props.a), reactive(props.a), still not working我试过 ref(props.a), reactive(props.a), 还是不行

1st call calcs, the output is props.a={x: 'y'}第一次调用计算,output 是 props.a={x: 'y'}
b={x: 'y',xxx: 'yyy'} b={x: 'y',xxx: 'yyy'}

2nd call calcs, the output become props.a={x: 'y',xxx: 'yyy'}第二次调用计算,output 变成 props.a={x: 'y',xxx: 'yyy'}
b={x: 'y',xxx: 'yyy',xxx: 'yyy'} b={x: 'y',xxx: 'yyy',xxx: 'yyy'}

3rd call calcs, the output become props.a={x: 'y',xxx: 'yyy',xxx: 'yyy'}第三次调用计算,output 变成 props.a={x: 'y',xxx: 'yyy',xxx: 'yyy'}
b={x: 'y',xxx: 'yyy',xxx: 'yyy',xxx: 'yyy'} b={x: 'y',xxx: 'yyy',xxx: 'yyy',xxx: 'yyy'}

I have no idea the prop a changes after each call, how can I get the output the same as the 1st call which is props.a={x: 'y'}我不知道每次调用后 prop a 都会发生变化,我怎样才能得到与第一次调用相同的 output props.a={x: 'y'}
b={x: 'y',xxx: 'yyy'} b={x: 'y',xxx: 'yyy'}

thanks, im new to vue.谢谢,我是 vue 的新手。

var b = JSON.parse(JSON.stringify(props.a)) fixed the issue. var b = JSON.parse(JSON.stringify(props.a)) 解决了这个问题。

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

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