Vue.js

组件之间传值之子给父传(事件)自定义事件 $emit

2018-09-21  本文已影响0人  天赐很棒

子给父传实例1:
body部分:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id='add'>
<my-father></my-father>
</div>
js部分:
<script src='js/vue.js'></script>
<script>
Vue.component("my-father",{
template:<div> <h1>{{mess}}</h1> <my-child @send='rcvMsg'></my-child> </div>,
data:function(){
return{
mess:''
}
},
methods:{
rcvMsg:function(txt){
this.mess=txt
}
}
})

   Vue.component('my-child',{
       template:`
            <button @click='sendToFather'>传给父元素</button>
         `,
       data:function(){
           return{
               msg:'我是子组件中的数据,要给父组件传值'
           }
       },
       methods:{
         sendToFather:function(){

// this.emit('自定义事件名',要传输的数据) this.emit('send',this.msg)
}
}
})
new Vue({
el:"#add"
})
</script>
</body>
</html>

4.png
子给父传小练习:
body部分:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="app">
<my-father></my-father>
</div>
js部分:
<script src="js/vue.js"></script>
<script>
Vue.component('my-father',{
template:<div> <h1>我是父组件</h1> <p>组件传过来的数据:<b>{{mess}}</b></p> <my-child @send='rcvMsg'></my-child> </div>,
data:function(){
return{
mess:''
}
},
methods:{
rcvMsg:function(txt){
this.mess=txt
}
}
})
    Vue.component('my-child',{
        template:`
            <div>
                <h1>我是子组件</h1>
                <input type="text" v-model='msg'> <button @click='sendMsg'>发送</button>
            </div>
        `,
        data:function(){
            return{
                msg:''
            }
        },
        methods:{
            sendMsg:function(){
                this.$emit('send',this.msg)
            }
        }
    })
    
    
    new Vue({
        el:'#app'
    })
</script>

</body>
</html>

5.png
组件购物车实例:
body部分:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="css/bootstrap.css">
</head>
<body>
<div id='app'>
<my-father></my-father>
</div>
js部分:
<script src='js/vue.js'></script>
<script>
Vue.component('my-father',{
template:<div class='container'> <table class='table table-bordered text-center'> <thead> <tr> <th class='text-center'>编号</th> <th class='text-center'>名称</th> <th class='text-center'>单价</th> <th class='text-center'>数量</th> <th class='text-center'>小计</th> </tr> </thead> <my-child v-bind:fruit='list'></my-child> </table> </div>,
data:function(){
return{
list:[
{pname:'apple',price:1,count:1,sub:1},
{pname:'pear',price:1,count:1,sub:1},
{pname:'orange',price:1,count:1,sub:1}
]
}
}
})
   Vue.component("my-child",{
       props:['fruit'],
       template:`
           <tbody>
              <tr v-for="(value,index) in fruit">
                 <td>{{index+1}}</td>
                 <td>{{value.pname}}</td>
                 <td>{{value.price}}</td>
                 <td>
                    <button @click='add(index)'>+</button>
                    <span>{{value.count}}</span>
                    <button @click="redu(index)">-</button>
                 </td>
                 <td>{{value.sub}}</td>
              </tr>
              <tr>
                  <td colspan=5>总价:{{sum}}</td>
              </tr>
           </tbody>
         `,
       data:function(){
           return{
               sum:0
           }
       },
       methods:{
           add:function(ind){
               this.fruit[ind].count++;
               this.fruit[ind].sub=this.fruit[ind].count*this.fruit[ind].price;
               this.countSum();
           },
           redu:function(ind){
               if(this.fruit[ind].count>1){
                   this.fruit[ind].count--;
              }   
               this.fruit[ind].sub=this.fruit[ind].count*this.fruit[ind].price;
               this.countSum();
           },
           countSum:function(){
                for(var i=0,total=0;i<this.fruit.length;i++){
                      total+=this.fruit[i].sub;
                }
               this.sum=total;
           }
       }
   }) 
    
    
    
    new Vue({
        el:'#app'
    })

</script>

</body>
</html>


6.png
上一篇 下一篇

猜你喜欢

热点阅读