美文网首页
2018-09-20子传父的案例

2018-09-20子传父的案例

作者: 萧声断未央 | 来源:发表于2018-09-22 08:58 被阅读0次
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
      <div id='app'>
          <my-father></my-father>
      </div>
       <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:"#app"
           })
        </script>
    </body>
    </html>








    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
       <div id='app'>
           <my-father></my-father>
       </div>
        <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>




    <!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>
        <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:5,count:5,sub:25},
                            {pname:'pear',price:4,count:4,sub:16},
                            {pname:'orange',price:3,count:3,sub:9}
                        ]
                    }
                }
            })
    
           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>

相关文章

网友评论

      本文标题:2018-09-20子传父的案例

      本文链接:https://www.haomeiwen.com/subject/fiptoftx.html