11-获取组件内容

<!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>获取组件内容</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
    <div id="app">
      <counter ref='one' @change='Add'></counter>
      <counter ref='two' @change='Add'></counter>
      <h1>总数{{total}}</h1>
    </div>
    <script>
        Vue.component('counter',{
            data:function(){
                return {
                    number:0
                }
            },
            template:'<div @click="counterClick">{{number}}</div>',
            methods:{
                counterClick:function(){
                    
                    this.number++;
                    console.log('当前数值:'+this.number);
                    // 子组件向父组件传值
                    this.$emit('change',this.number)
                }
            }
        })
        var app = new Vue({
            el:'#app',
            data:{
                total:0
            },
            methods: {
                Add:function(){
                        this.total = this.$refs.one.number +this.$refs.two.number;                       
                }
            },
        })
    
    </script>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/suni1024/p/11540215.html