Vue组件通信(父→子)和(子→父)

从父组件向子组件通信,通过props传递数据:

<div id="app">
    <my-component :message="msg"></my-component>
</div>
<script>
    Vue.component('my-component',{
        props:['message'],
        template:`<div>{{message}}</div>`
    })
    var app=new Vue({
        el:'#app',
        data:{
            msg:"Hello World!"
        }
    })
</script>

从子组件向父组件通信,通过子组件用$emit()触发事件,父组件用$on()来监听子组件的事件。

父组件也可以直接在子组件的自定义标签上使用v-on来监听子组件触发的自定义事件:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <p>总数:{{total}}</p>
        <my-component @increase="handleGetTotal" @reduce="handleGetTotal"></my-component>
    </div>

    <script src="./vue.js"></script>
    <script>
        Vue.component('my-component', {
            template: '\
            <div>\
            <button @click="handleIncrease">+1</button>\
            <button @click="handleReduce">-1</button>\
            </div>',
            methods: {
                handleIncrease: function() {
                    this.counter++;
                    this.$emit('increase', this.counter);
                },
                handleReduce: function() {
                    this.counter--;
                    this.$emit('reduce', this.counter);
                }
            },
            data: function() {
                return {
                    counter: 0
                }
            }
        })

        var app = new Vue({
            el: '#app',
            data: {
                total: 0
            },
            methods: {
                handleGetTotal: function(total) {
                    this.total = total;
                }
            }
        })
    </script>
</body>

</html>

在改变data中counter后,通过$emit()再把它传递给父组件,父组件用v-on:increase和v-on:reduce。

$emit()方法的第一个参数是自定义事件的名称,后面的参数都是要传递的数据,可以不填或填多个。 

除了上面的方式可以从子组件向父组件传值外,还可以使用v-model达到同样的效果:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <p>总数:{{total}}</p>
        <my-component v-model="total"></my-component>
    </div>

    <script src="/vue.js"></script>
    <script>
        Vue.component('my-component', {
            template: `
           <button @click="handleClick">+1</button>
            `,
            data: function() {
                return {
                    counter: 0
                }
            },
            methods: {
                handleClick: function() {
                    this.counter++;
                    this.$emit('input', this.counter);
                }
            }
        })

        var app = new Vue({
            el: '#app',
            data: {
                total: 0
            }
        })
    </script>
</body>

</html>

仍然是点击按钮加1的效果,不过这次组件$emit()的事件名是特殊的input。 

猜你喜欢

转载自blog.csdn.net/liangmengbk/article/details/85726006