vue Avoid mutating a prop directly since the value will be overwritten whenever the parent component

在使用vue 时报错:
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: “XXX”
报错原因是父组件通过props 传参给子组件,子组件又通过 e m i t 的 方 式 更 新 父 组 件 中 对 应 P r o p 绑 定 的 参 数 , 违 背 了 数 据 传 输 单 向 流 动 原 则 。 正 确 的 写 法 应 该 是 父 组 件 通 过 t h i s . emit的方式更新父组件中对应Prop绑定的参数,违背了数据传输单向流动原则。正确的写法应该是父组件通过this. emitPropthis.refs.XXX.XXX()的方式让子组件执行对应方法。
如在列子中父组件需要控制打开和关闭子组件中的dialog,应该做如下处理:

//子组件
<template>
    <el-dialog
     title="dialog"
    :visible.sync="visible"
    width="500">
    </el-dialog>
</template>

<script>
    export default {
      name: "list_form",
      data(){
        return{
            visible :false
        }
      },
      methods:{
        open(){
          this.visible = true
        }
      }
    }
</script>

<style scoped>

</style>
//父组件
<template>
<div>
  <div>
    <el-button type="primary" size="mini" @click="openFun">新增</el-button>
  </div>
  <el-table :data="list">
  </el-table>
  <list_form  ref="listForm"/>
</div>
</template>

<script>
  import List_form from "@/views/computer/list_form";
    export default {
      name: "list",
      components: {List_form},
      data(){
        return {
          list:[],
          former:{
            visible:false
          },
        }
      },
      methods:{
        openFun(){
            this.$refs.listForm.open()
        }
      }
    }
</script>
<style scoped>

</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值