VUE报错: Avoid mutating a prop directly since the value will be overwritten whenever the parent及解决方案

VUE报错:[Vue warn]: 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: "topActive"

大概意思是:避免直接改变属性,因为每当父组件重新渲染时,该值将被覆盖。相反,使用基于属性值的数据或计算属性。通过props传递给子组件的topActive,不能在子组件内部修改props中的topActive值。

<van-tabs
  class="top-tab"
  v-model="topActive"
  v-if="topData"
  @click="topChange"
  title-active-color="#fc5c5c"
  color="#fc5c5c"
>
  <van-tab v-for="(items, key) in topData" :key="key" :title="items.title" :name="items.key" />
</van-tabs>
<script>
export default {
  name: 'ChartTab',
  props: {
    topData: Array,
    topActive: {
      type: String,
      required: true,
    },
  },
}
</script>

修改:不直接使用该参数 通过temp接收该props中的参数 使用temp

<van-tabs
  class="top-tab"
  v-model="activeTopTemp"
  v-if="topData"
  @click="topChange"
  title-active-color="#fc5c5c"
  color="#fc5c5c"
>
  <van-tab v-for="(items, key) in topData" :key="key" :title="items.title" :name="items.key" />
</van-tabs>
<script>
export default {
  name: 'ChartTab',
  props: {
    topData: Array,
    topActive: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      // 避免直接修改props
      activeTopTemp: this.topActive,
    }
  },
}
</script>

扩展:

v-model通常用于input的双向数据绑定 <input v-model="parentMsg">,也可以实现子组件到父组件数据的双向数据绑定

:model是v-bind:model的缩写

<child :model="msg"></child>这种只是将父组件的数据传递到了子组件,并没有实现子组件和父组件数据的双向绑定。

引用类型除外,子组件改变引用类型的数据的话,父组件也会改变的。

 

解决vue 子组件修改父组件传来的props值报错问题

报错Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-

v-model和:model的区别

  • 27
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
该警告信息提醒我们避免直接改变一个prop的值,因为当父组件重新渲染时,该值将被覆盖。相反,我们应该使用基于prop值的数据或计算属性来进行操作。这个警告信息对两个prop也进行了具体的引用,分别是"placement"和"name"。 这个警告的目的是为了保证数据的一致性和可维护性。当我们直接改变一个prop的值时,父组件重新渲染时会将新的值覆盖之前的值,这可能会导致不可预测的结果和错误。通过使用数据或计算属性,我们可以在子组件中创建一个副本来进行操作,而不会影响到父组件的数据。这样可以确保数据的更新只在子组件内部进行,并且不会影响到父组件的状态。 一个解决这个警告的方法是在子组件中使用一个数据或计算属性来存储prop的值。这样,我们可以在子组件内部对该值进行改变,而不会影响到父组件。另外,我们还可以使用事件来通知父组件数据的变化,从而保持父子组件之间的数据同步。 另一个解决这个警告的方法是使用Vue提供的.sync修饰符。通过在子组件中使用.sync修饰符绑定prop值,我们可以在子组件中直接改变该值,而不会触发警告信息。这种方法可以简化代码,并提供更好的可读性。但需要注意的是,使用.sync修饰符不适用于所有的情况,需要根据具体的需求来决定是否使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-](https://blog.csdn.net/qq_47756657/article/details/125798183)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [解决报错Avoid mutating a prop directly since the value will be overwritten whenever the parent ...](https://blog.csdn.net/qq_52045491/article/details/129676506)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [解决vue 子组件修改父组件传来的props值报错问题](https://download.csdn.net/download/weixin_38534683/13678325)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值