2018-09-19vue組件運用(父給子傳)

運用組件添加刪除元素(父給子傳)

父給子傳(用props屬性傳值)

<!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>
                   <input type="text" v-model="inputVal"> <button @click='add'>添加</button>
                   <my-child v-bind:fruit='list'></my-child>
               </div>
             `,
            data:function(){
                return{
                    list:['apple','pear','orange'],
                    inputVal:''
                }
            },
            methods:{
                add:function(){
                   this.list.push(this.inputVal); 
                }
            }
        })
        
        Vue.component('my-child',{
            props:['fruit'],
            template:`
              <ul>
                 <li v-for="(value,index) in fruit">
                    {{value}}
                     <button @click="delt(index)">刪除</button>
                  </li>
              </ul>
           `,
            methods:{
                delt:function(ind){
                    this.fruit.splice(ind,1)
                }
            }
        })
        
        
        
        new Vue({
            el:'#app'
        })
    </script>
</body>
</html>
QQ拼音截圖未命名.png

運用組件做購物車效果

<!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'>數(shù)量</th>
                         <th class='text-center'>小計</th>
                       </tr>
                  </thead>
                   <my-child v-bind:product='goods'></my-child>
               </table>
             </div>
            `,
            data:function(){
                return{
                    goods:[
                        {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:['product'],
            template:`
                <tbody>
                  <tr v-for="(value,index) in product">
                     <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:""
                }
            },
            methods:{
                add:function(ind){
                    this.product[ind].count++;
                    //小計、
                    this.product[ind].sub=this.product[ind].count*this.product[ind].price;
                    this.countSum();
                 
                },
                redu:function(ind){
                    if(this.product[ind].count>1){
                       this.product[ind].count--;
                    }
                    //小計
                      this.product[ind].sub=this.product[ind].count*this.product[ind].price;
                     this.countSum();
                },
                countSum:function(){
                    for(var i=0,total=0;i<this.product.length;i++){
                        total+=this.product[i].sub;
                    }
                    this.sum=total;
                }
                
                
            }
        })
        
        
        new Vue({
            el:'#app'
        })
    </script>
</body>
</html>
QQ拼音截圖未命名.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、通過CocoaPods安裝項目名稱項目信息 AFNetworking網(wǎng)絡(luò)請求組件 FMDB本地數(shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,175評論 3 119
  • 有這么一個故事:有一年秋天,一群天鵝來到天鵝湖的一個小島上。它們從遙遠的北方飛來,準備去南方過冬。 島上住著老漁夫...
    弘謙學堂閱讀 367評論 0 0
  • 不知從何時起,只要一談起三國里的謀士,很多人就會說最厲害的是郭嘉! 仿佛只有這么說才能證明自己逼格很高。 我對此一...
    于是乎V閱讀 16,702評論 37 94
  • 看劇像是看盡了別人一生的酸甜苦辣喜怒哀樂。 感覺現(xiàn)實生活中很少會有夏明月這樣的人存在,永遠的無私永遠的寬容大度但卻...
    橘yu閱讀 1,652評論 0 3
  • 小暖男 晚上我們娘倆正在玩的起勁的時候,銘屁又開始要抱著我臉黏糊我的時候,我因為感冒就和他說:“媽媽感冒了,...
    十九八七_c494閱讀 359評論 0 0

友情鏈接更多精彩內(nèi)容