vue子组件更新父组件值

子组件更新父组件

父组件定义参数

<template>
  <view class="prepayment-trial-info">
  		//这里是需要更新的数据:mailForm.sync="mailForm",将会更新mailForm字段
    	<user-info-mail :mailForm.sync="mailForm"></user-info-mail>
  </view>
</template>

<script>
import userInfoMail from "../../../components/user-info-mail/user-info-mail.vue"
export default {
  data() {
    return {
      mailForm: {
        name: "",
      }
    }
  },
  methods: {
  },
  components: { userInfoMail }
}
</script>

<style scoped lang="scss">
.prepayment-trial-info {
 
}
</style>

子组件定义参数以及更新父组件参数

<template>
  <view class="user-info-mail">
    <view class="list">
      <view class="item item-border">
        <view class="left"> 收件人:</view>
        <view class="item-right ">
          <input type="text" class="right" placeholder="请输入收件人" v-model.trim="mailForm.name"/>
          <view class="img"></view>
        </view>
      </view>
  </view>
</template>
<script>
export default {
  data() {
    return {
      mailForm:{
        name:"",
      }
    }
  },
  watch:{
    mailForm:{
      handler: function() {
      //深度监听对象 当对象有改动时将更新父组件页面定义的mailForm数据
       this.$emit('update:mailForm',this.mailForm)
     },
     deep: true
    }
  },
  methods:{
  }
}
</script>

<style scoped lang="scss">
.user-info-mail {

  }
}
</style>

这样当在输入框输入值后,将会用watch监听变化,然后再去更新父组件的值。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值