[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent

[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: “app”

[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: "app"

这样的错误是我在昨天遇到的,今天反过来再看一遍官方文档,看到了解释。官方文档中文截图如下:在这里插入图片描述
英文截图如下:在这里插入图片描述

又有点新东西

今天再次遇到了这个问题,根据官方文档的翻译,我们可以理解为props中的属性最好避免改动,已经提醒你了,出错了算你自己的
如果我们一定要改动这些值,我们可以在data中重新定义相应的属性来接收props中的属性。例:

<template>
  <div>
    <span>子组件</span>
    <input type="text" name="" v-model.trim="textTip">
    <button type="button" class="btn btn-success btn-xs" v-on:click="callFather">通知父组件</button>
  </div>
</template>
<script>


export default {
  data() {
    return {
      textTip:this.propTextTip
    }
  },
   props:['propTextTip'],
  methods:{
      callFather:function() {
          //发射信号
          console.log(this.textTip);
          this.$emit('getCalled',this.textTip);
          //其中 getCalled 是一个自定义的事件,功能类似于一个中转
      }
  }
}

</script>

我们很明显的可以看到上述代码中,就用textTip接收了props中的propTextTip,这样可以成功的避免上述问题。
PS:感慨一下,不间断地返回来看看官方文档,还是很有收获的,平时的一些小错误,都是躺在官方文档里面。书读百遍其义自见,古人智慧,万古真理。

代码来源:https://www.cnblogs.com/cristina-guan/p/9269275.html

该警告信息提醒我们避免直接改变一个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 ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值