vue父子组件传值报错Avoid mutating a prop directly since the value will be overwritten whenever the parent co

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: “show”

这个错误的意思是在子组件直接修改了父组件传递过来的参数,打破了vue父子传值单向流的规则。

具体的做法是在子组件通过this.$emit定义方法,在父组件接受方法,来改变传递过来的参数。

//父组件代码

<script>
//子组件的位置
import Test from './test.vue'
export default {
  name: 'home',
  data() {
    return {
      show: false
    }
  },
}
</script>

<template>
<div>
  <Test :show="show"></Test>
</div>
</template>

<style scoped lang="less">

</style>


//子组件代码


<script>
export default {
  name: 'test',
  props: {
    show: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
    }
  },
  mounted() {
  },
  methods: {
//重要!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//这样一样可以关闭子组件,但是就会报刚才的错误
   // onclose(){
    //   this.show = false
    // },

//正确的做法
 //  子组件做下面的操作
    onclose(){
      this.$emit('onClose')
    }

}
</script>

<template>
<div>
  <div v-if="show">
    <div >我是子组件</div>
    <button @click="onclose">子组件关闭</button>
  </div>
</div>
</template>

<style scoped lang="less">

</style>





//修改后的父组件


<script>
//子组件的位置
import Test from './test.vue'
export default {
  name: 'home',
  data() {
    return {
      show: false
    }
  },
   methods: {
     onClose(){
     this.show = false
     },
  }
}
</script>

<template>
<div>
  <Test :show="show" @onClose='onClose'></Test>
</div>
</template>

<style scoped lang="less">

</style>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值