vue笔记9.20

2018-09-21  本文已影响0人  那年的雨

在vue中父组件向自子组件传递props
子组件向父组件传递属性是用$emit

shopping:

<!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:list='fruList'></my-child>
          </table>
        </div>
       `,
       data:function(){
           return{
               fruList:[
                   {pname:'apple',price:3,count:3,sub:9},
                   {pname:'pear',price:4,count:4,sub:16},
                   {pname:'orange',price:5,count:5,sub:25}
               ]
           }
       }
   }) 
   
  Vue.component('my-child',{
      props:['list'],
      template:`
         <tbody>
             <tr v-for="(value,index) in list">
                <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.list[ind].count++ ;
              //计算小计
              this.list[ind].sub=this.list[ind].count*this.list[ind].price;
              this.countSum();
          },
          redu:function(ind){
              if(this.list[ind].count>1){
                 this.list[ind].count--
              }  
               //计算小计
              this.list[ind].sub=this.list[ind].count*this.list[ind].price;
              this.countSum();
          },
          countSum:function(){
              for(var i=0,total=0;i<this.list.length;i++){
                  total+=this.list[i].sub;
              } 
              this.sum=total;
          }
      }
  }) 
   
   
   
   
   
   new Vue({
       el:'#app'
   })
</script>
</body>
</html>

浏览器打开:


图片.png

子传父:

<!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='revMsg'></my-child>
            </div>
         `,
        data:function(){
            return{
                mess:''
            }
        },
        methods:{
           revMsg:function(txt){
               this.mess=txt
           }
         }
    })
    
    Vue.component('my-child',{
        template:`
             <button @click='sendFather'>给父组件</button>
         `,
        data:function(){
            return{
                msg:'张世远是共产主义接班人'
            }
        },
        methods:{
            sendFather:function(){
//                   this.$emit('自定义事件',参数) 
                   this.$emit('send',this.msg)
            }
        }
    })
    
    
    new Vue({
        el:"#app"
    })
</script>
</body>
</html>

浏览器打开:


图片.png 图片.png

子组件给父组件传值:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
   <div id='app'>
       <father></father>
   </div>
    <script src='js/vue.js'></script>
    <script>
    Vue.component('father',{
        template:`
          <div>
            <h1>{{mess}}</h1>  
            <son @send='rcvMsg'></son>
          </div>
         `,
        data:function(){
            return{
               mess:'' 
            }
            
        },
        methods:{
            rcvMsg:function(txt){
                this.mess=txt;
            }
        }
    })
    
     Vue.component('son',{
        template:`
            <button @click='sendToFather'>我是子组件,我要向父组件传值</button>
          `,
         data:function(){
             return{
                 msg:'子组件向父组件传值'
             }
         },
         methods:{
             sendToFather:function(){
                 this.$emit('send',this.msg )
             }
         }
    })
   new Vue({
       el:"#app",
       
   })
</script>
</body>
</html>
上一篇下一篇

猜你喜欢

热点阅读