[英]Props being mutated in VueJS
I am new to vue and I am getting this error, I am not sure if I am passing the props right and executing it well in the other component.我是 vue 新手,我遇到了这个错误,我不确定我是否正确传递了道具并在其他组件中很好地执行它。 I will explain in details what I am trying to acheive.我将详细解释我想要实现的目标。
I am hiding a component on click on this page and showing another element on click interchangeably here.我在此页面上单击时隐藏了一个组件,并在此处可互换地在单击时显示另一个元素。
I have read a couple of solutions but I do not understand how I'm exactly suppose to fix it我已经阅读了几个解决方案,但我不明白我究竟应该如何解决它
<div v-if="hidden" class="orderSummary">
<div class="orderSummary__container">
<h2 class="orderSummary__header">Order Summary</h2>
<button @click="showForm()" class="total__button">Continue</button>
<PaymentForm v-if="!hidden" :hidden="hiddenMode" />
</div>
</div>
methods: {
showForm() {
if (this.subTotal > 1) {
this.hidden = false;
}
}
},
now in the Payment Form component I need to hide the component and make the other appear also, i want to do this by passing props.现在在支付表单组件中,我需要隐藏该组件并使另一个组件也出现,我想通过传递道具来做到这一点。 This is my code这是我的代码
<div class="payForm">
<div @click="hideForm()" class="PayForm__icon">
<backIcon class="icon" />
<span class="PayForm__icon-text">Go back</span>
</div>
</div>
props: ["base_amount", "value_added_tax", "hiddenMode"],
methods: {
submit() {
const data = {
name: this.name,
};
},
hideForm() {
this.hiddenMode = true;
}
},
I'm getting the error below, what do I do我收到以下错误,我该怎么办
Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "hiddenMode"
Don't make hiddenMode a prop;不要让 hiddenMode 成为道具; set it to state through data:通过数据将其设置为 state:
//parent
props: ["base_amount", "value_added_tax"],
data() {
return {
hiddenMode: false
}
},
...
edit:编辑:
You should also move hideForm() to the parent component and instead bind to PaymentForm's onlick:您还应该将 hideForm() 移动到父组件,而是绑定到 PaymentForm 的 onlick:
@click="$emit('clicked')"
then in the parent component bind hideForm to the clicked emit:然后在父组件中将 hideForm 绑定到点击的发射:
<PaymentForm v-if="!hidden" :hidden="hiddenMode" @clicked="hideForm"/>
Note: the $emit doesn't have to be called "clicked" you can name it anything注意:$emit 不必称为“点击”,您可以将其命名为任何名称
First, there is a logical problem here.首先,这里有一个逻辑问题。 If hidden is false then the first DIV and children including PaymentForm are not existing.如果 hidden 为 false,则第一个 DIV 和包括 PaymentForm 在内的子项不存在。 If hidden is true the PaymentForm not showing too because you have a <PaymentForm v-if="!hidden"
如果 hidden 为真,则 PaymentForm 也不会显示,因为您有一个<PaymentForm v-if="!hidden"
Second, your PaymentForm has a hiddenMode prop and you don't set it in the parent vue.其次,您的 PaymentForm 有一个 hiddenMode 道具,并且您没有在父 vue 中设置它。 You should have :hiddenMode="hidden"
and not :hidden="hiddenMode"
你应该有:hiddenMode="hidden"
而不是:hidden="hiddenMode"
For you hideForm function use $emit为你 hideForm function 使用 $emit
this.$emit('update:hiddenMode', true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.