Vue父子组件之间的通信

本文详细介绍了Vue.js中父子组件间的通信机制,包括父组件向子组件传递数据的方法及子组件通过自定义事件向父组件发送数据的过程。通过具体代码示例,展示了如何在Vue.js项目中实现组件间的数据交互。

如果你之前有了解过的话,那么只需要看前面这一部分!

终极思想

  • 父向子传值 把握中间值props

    • 子组件在props中创建一个属性,用以接收父组件传过来的值
    • 父组件中注册子组件
    • 在子组件标签中添加子组件props中创建的属性
    • 把需要传给子组件的值赋给该属性
  • 子向父传值 把握自定义函数的使用

    • 子组件中需要以某种方式例如点击事件的方法来触发一个自定义事件
    • 将需要传的值作为$emit的第二个参数,该值将作为实参传给响应自定义事件的方法
    • 在父组件中注册子组件并在子组件标签上绑定对自定义事件的监听

正文开始

父组件向子组件传递信息

<div id="app">
        <Counter :count="num"></Counter>
        <Counter :count="num"></Counter>
    </div>
    <template id="counter">
        <div>
            <button @click="n++">+</button>
            {{n}}
        </div>
    </template>
    <script>
        Vue.component('Counter', {
            props: ['count'],
            template: "#counter",
            data: function () {
                return {
                    n: this.count
                }
            }
        })
        new Vue({
            el: "#app",
            data: {
                num: 10
            }
        })
    </script>

结果输出:
在这里插入图片描述

子向父组件传值

<!-- Prop 作为初始值传入后,子组件想把它当作局部数据来用; -->
    <div id="app">
        <p>父级total:{{total}}</p>
        <button @click="incrementTotal">父级点击</button>
        <hr>
        <Counter v-on:increment="incrementTotal">

        </Counter>
    </div>

    <template id="counter">
        <div style="border:1px solid blue">
            <button @click="increment">子级加
                {{counter}}
            </button>
        </div>
    </template>
    <script>
        Vue.component('Counter', {
            template: "#counter",
            data:function(){
                return {counter:0}
            },
            methods:{
                increment:function(){
                    this.counter += 1;
                    this.$emit("increment")
                }
            }
        })
        var vm = new Vue({
            el: "#app",
            data: {
                total:0
            },
            methods:{
                incrementTotal:function(){
                    this.total += 2
                }
            }
        })
    </script>

输出结果:
在这里插入图片描述

综合应用

<div id="app">
    <com1 v-bind:parentmsg="msg" @func="getMsgFormSon"></com1>
  </div>
  
  <template id="tmpl">
    <div>
      <h1>这是子元素 --- {{ parentmsg }}</h1>
      <input type="button" value="向父组件传递消息" @click="sendMsg">
    </div>
  </template>

  <script>

    var com1 = {
      template: '#tmpl',
      data() {
        return {
          msg: '做一个孝顺的孩子,给爸爸一些钱去挥霍吧!'
        }
      },
      props: ['parentmsg'],
      methods: {
        sendMsg() {
          this.$emit('func', this.msg)
        }
      }
    }

    // 创建 Vue 实例,得到 ViewModel
    var vm = new Vue({
      el: '#app',
      data: {
        msg: '这是父组件中的数据,爸爸有100块钱,my son, 你要不',
        msgFormSon: ''
      },
      methods: {
        getMsgFormSon(data) {
          this.msgFormSon = data
          console.log(this.msgFormSon)
        }
      },
      components: {
        com1
      }
    });
  </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值