vue父子组件属性绑定的2种方式:“.sync“和“v-model“

4 篇文章 0 订阅

1.两种绑定方式比较

以父组件fatherValue属性绑定到子组件为例:

绑定方式version父组件子组件传值到父组件特点
.sync2.30+:father-value.sync=“fatherValue”fatherValue: { type: String, default: ‘’ }this.$emit(‘update:fatherValue’, this.childValue)可绑定到子组件任意属性
v-model2.20+v-model=“fatherValue”value: { type: String, default: ‘’ }this.$emit(‘input’, this.childValue)绑定到子组件value属性
2. 流程
  1. 父组件属性绑定到子组件
  2. 子组件触发事件
  3. 在子组件事件中,通过$emit 触发父组件的事件
3. [.sync] 修饰符

.sync 方式可以把父组件属性绑定到子组件任意属性上
eg: 以父组件fatherValue属性绑定到子组件fatherValue属性为例:
father.vue 父组件:

<template>
  <div>
    <table>
      <colgroup>
        <col width="30%">
        <col width="20%">
        <col width="20%">
        <col width="30%">
      </colgroup>
      <tr>
        <td></td>
        <td>
          fatherValue:
          <Input v-model="fatherValue" placeholder="fatherValue" />
        </td>
        <td>
          child组件值:
          <child :father-value.sync="fatherValue" />
        </td>
        <td></td>
      </tr>

    </table>
  </div>
</template>

<script>
import child from './Child'// 引入Child子组件
export default {
  name: 'Father',
  components: {
    child
  },
  data() {
    return {
      fatherValue: '35174'
    }
  }
}
</script>

Child.vue 子组件

<template>
  <div><Input v-model="childValue" @on-change="onChange" /></div>
</template>

<script>
export default {
  name: 'Child',
  props: {
    fatherValue: { type: String, default: '' }
  },
  data () {
    return {
      childValue: this.fatherValue
    }
  },
  methods: {
    onChange() {
      this.$emit('update:fatherValue', this.childValue)
    }
  }
}
</script>
4. v-model 方式

v-model 方式只能把父组件属性绑定到子组件value属性上。

<input v-model="fatherValue" />
<!-- v-model方式等于下面的代码 -->
<input :value="fatherValue" @input="fatherValue= $event.target.value" />

eg: 以父组件fatherValue属性绑定到子组件为例:
father.vue 父组件:

<template>
  <div>
    <table>
      <colgroup>
        <col width="30%">
        <col width="20%">
        <col width="20%">
        <col width="30%">
      </colgroup>
      <tr>
        <td></td>
        <td>
          fatherValue:
          <Input v-model="fatherValue" placeholder="fatherValue" />
        </td>
        <td>
          child组件值:
          <child v-model="fatherValue" />
        </td>
        <td></td>
      </tr>

    </table>
  </div>
</template>

<script>
import child from './Child'// 引入Child子组件
export default {
  name: 'Father',
  components: {
    child
  },
  data() {
    return {
      fatherValue: '35174'
    }
  }
}
</script>

Child.vue 子组件

<template>
  <div><Input v-model="childValue" @on-change="onChange" /></div>
</template>

<script>
export default {
  name: 'Child',
  props: {
    value: { type: String, default: '' }
  },
  data () {
    return {
      childValue: this.value
    }
  },
  methods: {
    onChange() {
      this.$emit('input', this.childValue)
    }
  }
}
</script>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
.sync修饰符与v-modelVue自定义组件中的区别是什么? .sync修饰符是用于实现组件组件传递数据并实时更新的功能。在正常的情况下,我们可以使用v-bind将数据传递给组件,但是在组件内部如果要修改这个数据,就需要通过$emit方法向组件发送事件并传递新的值。而使用.sync修饰符之后,我们可以直接在组件中使用v-model来接收组件传递的数据,并通过触发update事件来实时更新组件的数据。这样就省去了显式地使用$emit方法来发送事件的步骤。 例如,使用.sync修饰符的情况: <com1 :a.sync="num"></com1> 等价于 <com1 :a="num" @update:a="val=>num=val"></com1> 其中,num是组件中的数据,通过:a.sync将num传递给组件的a属性,并且监听update:a事件,当组件内部通过v-model修改了a的值时,会自动触发update:a事件,将新的值传递回组件并实时更新num的值。 而v-modelVue提供的一语法糖,用于实现双向数据绑定。通常情况下,我们在组件中使用v-model来接收组件传递的数据,并通过触发input事件来将新的值传递回组件。使用v-model的原理是侦听value属性和input事件。 例如,使用v-model的情况: <son v-model="num"></son> 等价于 <son :value="num" @input="val=>this.num=val"></son> 其中,num是组件中的数据,通过:value将num传递给组件的value属性,并且监听input事件,当组件内部通过$emit方法触发input事件时,将新的值传递回组件并实时更新num的值。 综上所述,.sync修饰符与v-model在实现父子组件之间数据的双向绑定上有所区别。.sync修饰符可以省去显式地使用$emit方法发送事件的步骤,而v-modelVue提供的一语法糖,用于简化双向数据绑定的写法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [详解VUE自定义组件中用.sync修饰符与v-model的区别](https://download.csdn.net/download/weixin_38629391/12758140)[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* [.sync与v-model的区别](https://blog.csdn.net/weixin_48837605/article/details/119616396)[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中 .sync 修饰符 和 v-model区别](https://blog.csdn.net/weixin_59229847/article/details/120815524)[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、付费专栏及课程。

余额充值