优雅的实现vue父子组件的value双向绑定

需求:父子组件value的双向绑定

  1. v-model可以拆分为@input和:value
  2. v-model实现双向绑定是@input的回调函数中,执行赋值给value的语法糖
  3. 子组件向父组件通讯,第一个参数为input,其实是给父组件中子组件@input回调中传入参数并且设置对应value
  4. 改变父组件中对应双向绑定的值,即可实现双向绑定
  5. 只能实现子组件最上级元素的value绑定,适用于父组件控制子组件显示隐藏等

父组件:

<template>
	<div id='testModel'>
		<button class="parent" @click="change">父组件按钮</button>
		<testChild v-model="isShow" :text="text"</testChild>
		
		<!-- 以下是demo解释 -->
		<input type="text" v-model="inputVlu">
		<!-- v-model可以做以下拆分 -->
		<input type="text" @input="ipt" :value="inputVlu">
		<!-- @input是v-on:input的语法糖 -->
		<input type="text" v-on:input="ipt" :value="inputVlu">
		
	</div>
</template>

<script>
  import testChild from './testChild';
    export default {
        name: "testModel",
      components:{testChild},
        data() {
            return {
              isShow:true,
              text:'哈哈哈',
              inputVlu:'inputVlu',
            }
        },
      methods:{
        change(){
          this.isShow=!this.isShow;
        }
      }
    }
</script>

<style lang="scss" type="text/scss" scoped>
    #testModel {

    }
</style>

子组件:

<template>
  <div id='testChild' :value="value" @input="setInput">
    <div class="box" v-show="value">{{text}}</div>
    <button @click="hid">子组件隐藏按钮</button>
  </div>
</template>

<script>
  export default {
    name: "testChild",
    data() {
      return {}
    },
    props: {
      value: {
        type: Boolean,
        redirect: true,
      },
      text:{
        type: String,
        default:'信息',
      },
    },
    methods: {
      setInput(value) {
        this.$emit('input', value);
        //向父组件传递数据,以input为对应函数key
        //父组件中v-on:ipnut中ipnut的回调接收这里的value值
      },
      hid(){
        this.$emit('input', !this.value);
      },
    },

  }
</script>

<style lang="scss" type="text/scss" scoped>
  #testChild {
    .box {
      width: 100px;
      height: 100px;
      background-color: pink;
    }
  }
</style>


Vue中,实现父子组件数据双向绑定可以通过props和$emit来实现。下面是一种常见的实现方式: 1. 在父组件中,通过props将数据传递给子组件。在子组件中,通过props接收父组件传递的数据。 2. 在子组件中,通过在子组件的data选项中定义一个与props同名的属性,用于接收父组件传递的数据。 3. 在子组件中,通过在模板中使用v-model指令将子组件的属性与父组件的数据进行绑定。这样,当子组件的属性发生变化时,父组件的数据也会相应地更新。 4. 在子组件中,通过使用$emit方法触发一个自定义事件,并将子组件的属性作为参数传递给父组件。 5. 在父组件中,通过在子组件标签上监听自定义事件,并在事件处理函数中更新父组件的数据。 下面是一个示例代码: ```html <!-- 父组件 --> <template> <div> <p>父组件数据:{{ parentData }}</p> <child-component v-model="parentData"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { parentData: '' }; } }; </script> <!-- 子组件 --> <template> <div> <input type="text" v-model="childData"> <button @click="updateParentData">更新父组件数据</button> </div> </template> <script> export default { props: ['value'], data() { return { childData: this.value }; }, methods: { updateParentData() { this.$emit('input', this.childData); } } }; </script> ``` 在上述示例中,父组件通过v-model将parentData与子组件的属性进行双向绑定。当子组件的输入框内容发生变化时,父组件的parentData也会相应地更新。而当点击子组件的按钮时,子组件会通过$emit方法触发一个名为input的自定义事件,并将子组件的属性childData作为参数传递给父组件。父组件通过监听该自定义事件,并在事件处理函数中更新parentData,从而实现父子组件数据的双向绑定
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值