账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    Vue里面子组件是如何通过自定义事件向父组件传值
    46
    0

    题目描述

    《Vue.js实战》里面的关于组件通信的章节,有一篇关于介绍自定义事件的。子组件的自定义事件通过$emit通知父组件,而父组件可通过v-on监听子组件的自定义事件。虽然知道,是这样的一个机制,但是看到下面的代码之后就还是没理解实质。希望大神能够解释一下,最好形象一些。

    题目来源及自己的思路

    《Vue.js实战》第7章 7.3节组件通信 7.3.1 自定义事件

    <my-component @increase="handleGetTotal" @reduce="handleGetTotal"></my-component>

    看到这里我是理解成这样:自定义标签my-component定义了两个自定义事件increase和reduce,这两个事件的事件处理程序都是handleGetTotal,所以我就去了子组件内查这个处理程序,但是发现子组件的methods里面没有,只有handleIncrease和handleReduce,这里感觉困惑。然后想这个是父组件通过v-on监听这两个事件,所以事件处理程序handleGetTotal应该是父组件提供?然后看到确实在父组件的methods里面找到了。所以,虽然increase和reduce是子组件自已定义的事件,但是对应的处理程序(这些事件都干些什么)一定是父组件提供?这个理解对么?

    然后再看详细的函数内容:

    handleGetTotal: function(total) {
                    this.total = total;
                    console.log(this);  //Vue
                }

    function(total)这里的total看起来似乎是从子组件那边传过来的,似乎是通过

    this.$emit('increase', this.counter);
    this.$emit('reduce', this.counter);

    但是,到底是不是这样传的?如果是这样传的 this.counter 作为子组件的数据怎么和function(total)这里的total对应起来的?我测试function(total)这里的参数是任意字符串都可以,所以对应起来:

    @increase="handleGetTotal"
    this.$emit('increase', this.counter);
    
    
    @reduce="handleGetTotal"
    this.$emit('reduce', this.counter);

    this.counter作为子组件处理之后的数据又回传给父组件提供的函数handleGetTotal。是不是这样的呢?
    如果是这样的话,前面的疑问似乎就是解了?

    相关代码

    // 请把代码文本粘贴到下方(请勿用图片代替代码)

      <div id="app" v-cloak>
            <p>总数:{{total}}</p>
            <my-component @increase="handleGetTotal" @reduce="handleGetTotal"></my-component>
      </div>
        
        <script src="https://unpkg.com/vue/dist/vue.js"></script>
        <script>
        Vue.component('my-component', {
            template: `\
            <div>\
                <button @click="handleIncrease">+1</button>\
                <button @click="handleReduce">-1</button>\
             </div>`,
            data: function() {
                return {
                    counter: 0
                }
            },
            methods: {
                handleIncrease: function() {
                    this.counter++;
                    this.$emit('increase', this.counter);
                    console.log(this);  //VueComponent
                },
                handleReduce: function() {
                    this.counter--;
                    this.$emit('reduce', this.counter);
                    console.log(this);   //VueComponent
                },
            }
        })
        var app = new Vue({
            el: '#app',
            data: {
                total: 0
            },
            methods: {
                handleGetTotal: function(total) {
                    this.total = total;
                    console.log(this);  //Vue
                }
            }
        })
        </script>
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 在Vue中,子组件可以通过自定义事件向父组件传值。这可以通过使用自定义事件处理器来实现。

      以下是一个简单的例子:

      首先,定义一个子组件:

      ```html

      ```

      然后,定义父组件:

      ```html

      ```

      在父组件中,我们可以通过添加@child事件处理器来触发子组件的事件处理器:

      ```html

      ```

      现在,每当子组件的@child事件被触发时,handleChildEvent方法将被调用,并将事件传递给child组件的@child事件处理器。handleChildEvent方法将打印出事件处理程序传递的值。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部