元件(子傳父)

weixin_34148340發表於2018-09-24
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="app">
    <my></my>
</div>
<script src="js/vue.js"></script>
<script>
    Vue.component('my',{
        template:`
            <div>
              <p>總:{{counter1}}</p>
              <my1 @increase="lan" @reduce="lan"></my1>
            </div>

        `,
        data:function(){
            return{
                counter1:0
            }
        },
        methods:{
            lan:function(txt){
                this.counter1=txt
            }
        }
    })





    Vue.component('my1',{
        template:`
            <div>
               <button @click="wang">+1</button>
               <button @click="nuo">-1</button>
            </div>

        `,
        data:function(){
            return{
                counter:0
            }
        },
        methods:{
            wang:function(){
                this.counter++;
                this.$emit('increase',this.counter)
            },
            nuo:function(){
                this.counter--;
                this.$emit('reduce',this.counter)
            }
        }
    })




    new Vue({
        el:'#app'
    })
</script>
</body>
</html>

相關文章