【发布时间】:2020-12-14 05:22:42
【问题描述】:
我正在从父组件发送一个道具:user。现在在子组件中,我想复制它而不改变道具的值。
我试着这样做:
export default defineComponent({
props: {
apiUser: {
required: true,
type: Object
}
},
setup(props) {
const user = ref(props.apiUser);
return { user };
}
});
但是,如果我更改用户对象的值,它也会更改 apiUser 属性。我想也许使用 Object.assign 会起作用,但是 ref 不再是反应性的了。
在 Vue 2.0 中,我会这样做:
export default {
props: {
apiUser: {
required: true,
type: Object
}
},
data() {
return {
user: {}
}
},
mounted() {
this.user = this.apiUser;
// Now I can use this.user without changing this.apiUser's value.
}
};
感谢@buttons 的评论导致答案。
const user = reactive({ ...props.apiUser });
【问题讨论】:
-
好吧,它不会是反应式的,但你不能 Object.assign() / JSON.parse(JSON.stringify()) / lodash cloneDeep() 用户,添加数据引用,对此进行更改,然后 $emit 将更改返回给父级,您将在其中通过合并或 $set 反应性地合并更改,然后将其作为道具推回原处?我意识到您正在使用新的组合 API,而且我无论如何都不是 Vue 3 专家,但这至少是我一直这样做的方式,而 Vue 2 用于大型应用程序。
-
@Abarth,真的吗?这似乎是一种低效的方法。我希望有人知道一种更有效的方法。无论如何,谢谢!
-
如果你想复制道具,你必须使用 Object.assign 或类似的东西。真正的问题是,你到底想做什么?为什么需要道具副本?
-
我当然很乐意听到更好的方法。我将在下面添加我的方法作为答案,显然忽略它,如果结果不是答案,但至少我觉得我已经解释了自己。
-
啊,我的错。对于对象值,建议使用
reactive。很确定这不会引发类型错误。尝试使用const user = reactive(...props.apiUser);。这里是使用 ref vs reactive 的时间。
标签: javascript vue.js vue-composition-api