vue中父子组件通信的实例

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>vue父子组件传值</title>
  <script src="vue.js"></script>
</head>
<body>
<div id="app">
  <counter :count="0" @inc="handleIncreaseClick"></counter>
  <counter :count="1" @inc="handleIncreaseClick"></counter>
  <div>{
    
    {
    
    total}}</div>
</div>
<script>
  var counter={
    
    
    props:['count'],
    template:'<div @click="handleClick">{
    
    {number}}</div>',
    data(){
    
    
      return {
    
    
        number:this.count
      }
    },
    methods:{
    
    
      handleClick:function(){
    
    
        // this.count++;
        this.number=this.number+2;
        this.$emit('inc',2);
      }
    }
  };
  var app=new Vue({
    
    
    el:'#app',
    components:{
    
    
      counter
    },
    data:{
    
    
      total:1
    },
    methods:{
    
    
      handleIncreaseClick(step){
    
    
        this.total+=step
      }
    }
  })
</script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/tozeroblog/article/details/85141532