父组件和子组件数据双向绑定

 <!-- 封装 -->
              <el-descriptions-item label="分机机构">
                <wb-select :parentValue.sync="dadeval" placeholders="分机机构"></wb-select>
              </el-descriptions-item>
export default {
    data() {
      return {
        dadeval:"",
        }
     }
}

子组件

<template>
  <div>
    <el-popover placement="bottom" :width="width" trigger="click" popper-class="wb-padding" v-model="visible">
      <div>
        <div style="overflow: auto;height: 230px;">
          <ul class="el-scrollbar__view el-select-dropdown__list">
            <div v-for="(it,index) in list" :key="index" @click="upData(it)"><li class="el-select-dropdown__item"><span>{{it.name}}</span></li></div>
          </ul>
        </div>
        <div :style="'background-color: #fff;width:'+width+' px;'">
          <div style="display: flex; line-height: 30px;cursor: pointer;">
            <div class="addBorder" style="width: 50%;">新增</div>
            <div class="delBorder" style="width: 50%;">重置</div>
          </div>
        </div>
      </div>
      <el-input type="text" ref="myInput" v-model="name" @focus="focusSelect" @input="remoteName" slot="reference" size="mini" style="width: 100%;" :placeholder="placeholders"></el-input>
    </el-popover>
  </div>
</template>

<script>
export default {
  props: {
    parentValue: "",
    placeholders:"",
  },
  data() {
    return {
      name: '',
      width:"200",
      dataList:{"mshopid":""},
      visible: false,
      list: [{'id':1,"name":"dade"},{'id':2,"name":"dade2"},{'id':3,"name":"dade3"},{'id':4,"name":"dade4"}]
    };
  },
  mounted() {

  },
  methods: {
    // 获得焦点
    focusSelect(){
      const inputElement = this.$refs.myInput.$el;
      this.width = inputElement.offsetWidth;
      console.log(`el-input 的宽度为:${this.width}px`);
    },
    // 数据改变
    remoteName(e){
      console.log(this.placeholders)
    },
    //点击选中,返回值
    upData(it){
      this.name = it.name;
      this.$emit('update:parentValue', it.id);
      this.visible = false;
    }
  }
};
</script>
</script>

<style>
  .el-select-dropdown__wrap {
      max-height: 200px !important;
  }
  .wb-padding {
    padding: 0px;
  }
  .addBorder{
    text-align: center;
    border-top: 1px solid rgb(221 221 221);
  }
  .delBorder{
    text-align: center;
    border-top: 1px solid rgb(221 221 221);
    border-left: 1px solid rgb(221 221 221);
  }
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在 React 中,组件向子组件传递数据是通过 props 来实现的,而子组件组件传递数据通常是通过回调函数来实现的。 要实现子组件组件传递数据双向绑定,可以通过以下步骤实现: 1. 在组件中定义一个状态变量,并将其作为 props 传递给子组件。 2. 在子组件中,通过 props 获取传递过来的数据,并将其保存在子组件的状态变量中。 3. 在子组件中,通过事件处理函数修改子组件状态变量的值。 4. 在子组件中定义一个回调函数,将修改后的值通过该回调函数传递给组件。 5. 在组件中,接收子组件传递过来的值,并更新组件的状态。 这样,当子组件的状态变量发生改变时,会触发回调函数将最新的值传递给组件,实现了双向数据绑定。 以下是一个示例代码: ```jsx // 组件 import React, { useState } from 'react'; import ChildComponent from './ChildComponent'; function ParentComponent() { const [data, setData] = useState(''); const handleDataChange = (newData) => { setData(newData); }; return ( <div> <ChildComponent data={data} onDataChange={handleDataChange} /> </div> ); } export default ParentComponent; // 子组件 import React, { useState } from 'react'; function ChildComponent({ data, onDataChange }) { const [inputValue, setInputValue] = useState(''); const handleInputChange = (e) => { setInputValue(e.target.value); }; const handleButtonClick = () => { onDataChange(inputValue); }; return ( <div> <input type="text" value={inputValue} onChange={handleInputChange} /> <button onClick={handleButtonClick}>传递数据组件</button> </div> ); } export default ChildComponent;``` 在上面的示例中,组件 `ParentComponent` 定义了一个状态变量 `data`,并将其作为 props 传递给子组件 `ChildComponent`。子组件中,通过 `props` 获取 `data` 的值,并将其保存在子组件的状态变量 `inputValue` 中。当子组件输入框的值改变时,会触发 `handleInputChange` 来更新 `inputValue` 的值。当按钮被点击时,会调用 `handleButtonClick` 来触发回调函数 `onDataChange`,将最新的值传递给组件进行更新。这样就实现了子组件组件传递数据双向绑定
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大得369

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值