Vue2父子组件间的通信

父组件通过 props 向下传递数据给子组件,子组件通过 events 向上给父组件发送消息。


父组件:
<div>
    <div style="background:#34495E;color: #fff; padding:20px">
      <p style="margin-bottom: 20px">这是父组件</p>
      <div style="background:#E74C3C;color: #fff; padding:20px; margin-top:20px">
        <p>接受来自子组件的内容: </p>
        <p style="margin-top:20px;background:#fff; color:#000; padding:5px; line-height:1.5;">{{hello}}</p>
      </div>
    </div>
    <div style="background:#34495E;color: #fff; padding:20px; margin-top:20px">
      <p style="margin-bottom: 20px">这是子组件</p>
      <musicsearch @trans="transContent" :pupu="hello" :info="info"></musicsearch>
    </div>
  </div>
export default {
  components: {
    musicsearch
  },
  data() {
    return {
      hello: ‘‘,
      info: ‘‘
    }
  },
  methods: {
    transContent(msgs) {
      this.hello = msgs;
      this.info = msgs;
    }
  }
}

  

子组件:
<div>
    <div style="margin-top:20px; background:#E74C3C; padding:10px;">
      <input type="text" ref="ipt" style="border:none; margin-top:10px; margin-bottom: 20px; border-radius:4px; width:90%; height:18px; padding:5px; line-height:18px; border:1px solid #fff">
      <button @click="sendVal()" style="margin-bottom: 20px; border:none; outline:none; border-radius:4px; height:28px; line-height:28px; background:#F1C40F; color:#fff;">向父组件发送内容按钮</button>
    </div>
    <div style="margin-top:20px; background:#E74C3C; padding:10px;">
      <button @click="click()" style=" margin-top:10px; border:none; outline:none; border-radius:4px; height:28px; line-height:28px; background:#F1C40F; color:#fff;">接受来自父组件的内容按钮</button>
      <div style="margin-top:20px;background:#fff; color:#000; padding:5px; line-height:1.5;">{{msg}}</div>
    </div>
  </div>

export default {
  name: ‘girl-group‘,
  props: {
    info: ‘‘
  },
  data() {
    return {
      msg: ‘‘
    }
  },
  methods: {
    sendVal() {
      this.$emit(‘trans‘, this.$refs.ipt.value);
    //这里在父组件使用v-on来监听子组件上的自定义事件($emit的变化),一旦发生变化click方法里的值就会跟着改变,调用click事件可看到信息
    },
    click() {
      this.msg = this.info;
    }
  }
}

  

  

  

  

时间: 2024-08-29 19:06:40

Vue2父子组件间的通信的相关文章

第四节:Vue表单标签和组件的基本用法,父子组件间的通信

vue表单标签和组件的基本用法,父子组件间的通信,直接看例子吧. <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="vue.js"></script> </head> <body> <div id="app"&

小程序中父子组件间的通信与事件

点此查看微信小程序官方文档 以下示例,可自行体会.. 子 - Component  child.json { "component": true, "usingComponents": {} } child.wxml <view class='template-child'> <block wx:for='{{dataFromParent}}'> <button data-id='{{item.id}}' bindtap='onTapC

非父子组件间通信

非父子组件之间的通信,可以通过一个空的 Vue 实例作为中央事件总线(事件中心),用他来触发事件和监听事件. 在这里,如果是工作中的新手看了官网的例子直接上手写,会有些发懵.这个作为事件总线空的 Vue 实例我该写哪里去?因为工作中我们的组件都是互相独立的,不可能写一起的,作用域是不同的,所以需要稍作调整 一.在 main.js 中初始化根 Vue 之前,添加一个 data 对象,内写入一个名为 Event 的空 Vue 对象,也就是中央事件总线 new Vue({ el: '#app', da

父子组件间的传值

父子组件间通信 父组件-->子组件 1.父组件通过给子组件添加属性给子组件传值,子组件通过props来接受. 2但是要在属性前加“:”,即使用v-bind指令,才能使属性值是js代码,不然就是字符串. 3.单项输出流.即子组件不允许修改父组件传递的值,只能使用,不能修改. 父组件通过属性向子组件传值时,如果传递的是引用型的值,这时候子组件对父组件传递的值进行修改可能会影响其他子组件对这个引用类型值的使用. 解决办法:子组件在data项中新赋值父组件的变量,操作新的变量即可. 子组件-->父组件

React 组件基本使用(3) ---父子组件之间的通信

当有多个组件需要共享状态的时候,这就需要把状态放到这些组件共有的父组件中,相应地,这些组件就变成了子组件,从而涉及到父子组件之间的通信.父组件通过props 给子组件传递数据,子组件则是通过调用父组件传给它的函数给父组件传递数据. 很简单的一个例子,我们在输入框中输入温度,输入框下面显示冷和热.这里就有两个组件,输入框和它下面的显示内容,且它们共享一个状态,就是温度.所以我们要把温度这个状态放到这两个组件的父组件中.这里就有三个组件,一个输入框TemperatureInput,  一个是显示内容

【转】vue父子组件之间的通信

vue父子组件之间的通信 在vue组件通信中其中最常见通信方式就是父子组件之中的通性,而父子组件的设定方式在不同情况下又各有不同.最常见的就是父组件为控制组件子组件为视图组件.父组件传递数据给子组件使用,遇到业务逻辑操作时子组件触发父组件的自定义事件.无论哪种组织方式父子组件的通信方式都是大同小异. 父组件到子组件通讯 父组件到子组件的通讯主要为:子组件接受使用父组件的数据,这里的数据包括属性和方法(String,Number,Boolean,Object, Array ,Function).v

非父子组件间的传值

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>非父子组件间的传值(Bus/总线/发布订阅模式/观察者模式)</title> <script src="./vue.js"></script> </head> <body> <div

Vue入门七、父子组件间通讯

一.父子组件通讯 父传子:1.父用子的时候通过属性传递2.子要声明props:['属性名']接收3.子组件template中直接用 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script type="

vue的父子组件间传值以及非父子间传值

Vue组件间的传值方式: 父传子 子传父 非父子间传值 1.首先父组件传值给子组件,使用bind绑定属性,然后在子组件用props接收属性值. //父组件 <template> <input type="text" /> <br/> <child :inputValue="inputValue"></child> </template> <script> import child f