<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>test</title>
    <script src="./vue.js"></script>
    <style>

    </style>

</head>

<body>
    <div id="root">
        <counter ref="one" @change='handleChange'></counter>
        <counter ref="two" @change='handleChange'></counter>
        <div>{{total}}</div>
    </div>

    <script>
        Vue.component('counter', {
            template: '<div @click="handleClick">{{number}}</div>',
            data: function() {
                return {
                    number: 0
                }
            },
            methods: {
                handleClick: function() {
                    this.number++;
                    this.$emit('change');
                }
            }
        })
        var app = new Vue({
            el: " #root ",
            data: {
                total: 0,
            },
            methods: {
                handleChange: function() {
                    // console.log(this.$refs.one.number)
                    this.total = this.$refs.one.number + this.$refs.two.number;
                }
            }
        })
    </script>
</body>

</html>

说明 :

1.求和

2.使用ref

3.子组件的data是个函数

Logo

前往低代码交流专区

更多推荐