vue父子组件互相传值

本文详细介绍了四种在Vue组件间动态传递值的方法:v-bind、.sync、v-model和使用watch配合computed。通过实例演示了如何在父组件改变时更新子组件,并展示了不同方式的适用场景和代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一种:父组件用 :变量名="" @方法

父组件传递值给子组件

用 v-bind:inputName传递一个值

  <date-pick :inputName="item.label" @changeInputName="changeName($event,item)"></date-pick> 
  //这里传item是因为传item,然后修改里面的属性可以有一个动态的数据修改。自己把握

父组件中定义changeName方法

changeName (val, item) {
      item.label = val
}

子组件获取父组件的值

 props: ['inputName']

子组件在data上定义数据 fatherInputName

注意这里fatherInputName要用从Props里的数据来初始化

  data () {
    return {
      fatherInputName: this.inputName
      }
  }

在子组件中监听fatherInputName的改变

 watch: {
    fatherInputName (nval) {
      this.$emit('changeInputName', nval) // 这里和父组件上的@changeInputName保持一致啊,默认的第一个参数一定是$event
    }
  }

第二种: .inputName.sync方法

父组件传递给子组件 用sync

  <date-pick :inputName.sync="item.label"></date-pick>

子组件获取也是用props

props: ['inputName'],

一样用data定义个参数 fatherInputName

 fatherInputName: this.inputName,

监听参数fatherInputName

  watch: {
    fatherInputName (nval) {
      this.$emit('update:inputName', nval)
    }
  },

第三种: 在自定义组件上用v-model

     <date-pick v-model="item.label"></date-pick>

在子组件Props中用value接收,input发送事件

props: {
    value: {
      type: String
    }
  }

子组件中定义自己的数据 data

fatherInputName: this.value,

子组件利用watch监听 子组件中表单v-model改变,从而发给父组件让父组件发生改变

 watch: {
    fatherInputName(nval){
      this.$emit("input",nval)
    }
  },

子组件的template用刚刚定义的fatherInputName

          <el-input v-model="fatherInputName"></el-input>

第四种:上面都是用的watch,实际也可以使用computed来操作,也就是前3种都可以配合computed组合成6种

父组件传值给子组件

    <date-pick :inputName="item.label" @changeInputName="changeName($event,item)"></date-pick>

子组件用props接受值

props: ['inputName'],

子组件template , v-model的是computed的数据

 <el-input v-model="fname"></el-input>

子组件中定义数据

data () {
    return {
      fatherInputName: this.inputName,
      // 控住设置时段的显示与隐藏
      dialogVisible: false,
      }
 }

子组件中用computed去改变值

 computed:{
    fname: {
      set: function (nval) {
        this.$emit('changeInputName', nval)
        this.fatherInputName = nval
      },
      get: function () {
        return this.fatherInputName
      }
    }
  },
### Vue.js 父子组件方法 #### 使用 Props 和 $emit 进行单向数据流通信 在Vue中,父子组件间的数据递遵循单向下行流动的原则。父组件通过`props`向下递数据至子组件,在此过程中可以递任何类型的 JavaScript 数据结构,包括但不限于字符串、数、布尔、数组和对象[^2]。 对于子组件来说,如果需要与父组件沟通,则通常会触发事件来通知父组件某些状态的变化或请求操作。这可通过`$emit()`函数实现,该函数允许子组件发射自定义事件并携带参数返回给监听这些事件的父级组件[^3]。 下面给出具体的代码示例: #### 示例:父组件向子组件发送消息并通过按钮点击反馈回数 ##### 子组件 (ChildComponent.vue) ```html <template> <div class=""> <h2>我是子组件</h2> <!-- 显示来自父组件的消息 --> <p>{{ messageFromParent }}</p> <button @click="sendMessageToParent">向父组件</button> </div> </template> <script> export default { props: ['messageFromParent'], // 接收父组件来的内容 methods: { sendMessageToParent() { // 发射一个名为 'child-msg' 的事件并将当前计数器作为载荷发出 this.$emit('child-msg', "这是从子组件发来的问候"); } } } </script> ``` ##### 父组件 (ParentComponent.vue) ```html <template> <div id="parent"> <h1>这里是父组件</h1> <!-- 向子组件递信息,并绑定处理程序等待接收回应 --> <child-component :message-from-parent="'你好啊,子组件!'" v-on:child-msg="handleMessage" ></child-component> <hr /> <strong>收到的信息:</strong><span>{{ receivedMsg }}</span> </div> </template> <script> import ChildComponent from './components/ChildComponent'; export default { name: 'Parent', components: { ChildComponent, }, data() { return { receivedMsg: '' }; }, methods: { handleMessage(msg) { console.log(`接收到子组件的消息: ${msg}`); // 更新本地变量存储子组件过来的新消息 this.receivedMsg = msg; } } }; </script> ``` 在这个例子中,当用户点击子组件内的按钮时,将会调用`sendMessageToParent`方法,进而触发`$emit('child-msg')`事件;与此同时,父组件已经绑定了相应的处理器`v-on:child-msg="handleMessage"`,因此能够捕获到这条消息并更新自身的视图显示。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值