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

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

在这里插入图片描述
一个报错记录 大概意思是不可以直接修改父组件传过来的值
我这里写的是不对的 涉及公司内容 不全贴代码 只贴问题

props: {
    value: {
      type: Boolean,
      default: false
    },
methods: {
    valueChange (newVal) {
      this.value = newVal
      this.$emit('input', newVal)
    },

这里的value是父组件传过来的 不能直接修改
可以用$emit 来触发父组件函数修改value 这里我写个例子

父组件
<hhh :title="value" @changeValue="changeValue"></hhh>
 
子组件
<template>
  <div  @click="changeV">
     {{title}}
  </div>
</template>
 
//这是一个弹窗
 
<script>
    export default {
        name: "hhh",
        props:{
          title:"一个例子",
        },
      
        methods:{
          changeV(){
            //this.title = "改变title";  //这行去掉 不能直接改变props中参数的值
            this.$emit('changeValue',"改变它吧"); //通知父组件改变。
          }
        }
    }
</script>

父组件的changeValue 函数会拿到自组件传来的值 改变value

changeValue(emit){
  this.value  = emit
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值