vue筆記9.20

在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'>數(shù)量</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:'張世遠是共產(chǎn)主義接班人'
            }
        },
        methods:{
            sendFather:function(){
//                   this.$emit('自定義事件',參數(shù)) 
                   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>
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末眯漩,一起剝皮案震驚了整個濱河市汇在,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖弟灼,帶你破解...
    沈念sama閱讀 219,188評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件惰爬,死亡現(xiàn)場離奇詭異,居然都是意外死亡帅涂,警方通過查閱死者的電腦和手機织咧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來漠秋,“玉大人笙蒙,你說我怎么就攤上這事∏旖酰” “怎么了捅位?”我有些...
    開封第一講書人閱讀 165,562評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長搂抒。 經(jīng)常有香客問我艇搀,道長,這世上最難降的妖魔是什么求晶? 我笑而不...
    開封第一講書人閱讀 58,893評論 1 295
  • 正文 為了忘掉前任焰雕,我火速辦了婚禮,結果婚禮上芳杏,老公的妹妹穿的比我還像新娘矩屁。我一直安慰自己,他們只是感情好爵赵,可當我...
    茶點故事閱讀 67,917評論 6 392
  • 文/花漫 我一把揭開白布吝秕。 她就那樣靜靜地躺著,像睡著了一般空幻。 火紅的嫁衣襯著肌膚如雪烁峭。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,708評論 1 305
  • 那天秕铛,我揣著相機與錄音约郁,去河邊找鬼。 笑死但两,一個胖子當著我的面吹牛鬓梅,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播镜遣,決...
    沈念sama閱讀 40,430評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼己肮,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了悲关?” 一聲冷哼從身側響起谎僻,我...
    開封第一講書人閱讀 39,342評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎寓辱,沒想到半個月后艘绍,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,801評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡秫筏,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,976評論 3 337
  • 正文 我和宋清朗相戀三年诱鞠,在試婚紗的時候發(fā)現(xiàn)自己被綠了挎挖。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,115評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡航夺,死狀恐怖蕉朵,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情阳掐,我是刑警寧澤始衅,帶...
    沈念sama閱讀 35,804評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站缭保,受9級特大地震影響汛闸,放射性物質發(fā)生泄漏。R本人自食惡果不足惜艺骂,卻給世界環(huán)境...
    茶點故事閱讀 41,458評論 3 331
  • 文/蒙蒙 一诸老、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧钳恕,春花似錦别伏、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽宦芦。三九已至宙址,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間调卑,已是汗流浹背抡砂。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留恬涧,地道東北人注益。 一個月前我還...
    沈念sama閱讀 48,365評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像溯捆,于是被迫代替她去往敵國和親丑搔。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,055評論 2 355

推薦閱讀更多精彩內(nèi)容