組件

1、什么是組件识椰?

組件(component)是Vue最強大的功能之一绝葡,組件化開發(fā),組件可以擴展HTML元素腹鹉,封裝可重用的代碼藏畅。組件分為全局組件和局部組件。

2种蘸、全局組件

全局組件的書寫格式:
Vue.component("組件名"墓赴,{
       template:`
       <模板>
          `
})
例:
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>      
        <div id="app">
            <my-component></my-component>
        </div>
        <script src="js/vue.js"></script>
        <script>
            Vue.component("my-component",{
                template:`
                  <ul>
                    <li>蘋果</li>
                    <li>橘子</li>
                    <li>香蕉</li>
                    <li>柚子</li>
                    <li>葡萄</li>
                  </ul>
                `
            }),
            new Vue({
                el:"#app"
            })
        </script>
    </body>
</html>

3、局部組件

局部組件的書寫格式:
component("組件名"航瞭,{
     template:`
      <模板>
         `
})
例:
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>      
        <div id="app">
            <my-component></my-component>
        </div>
        <script src="js/vue.js"></script>
        <script>
            new Vue({
                el:"#app",
                components:{
                    "my-component":{
                        template:`
                          <ul>
                            <li>蘋果</li>
                            <li>橘子</li>
                            <li>香蕉</li>
                            <li>柚子</li>
                            <li>葡萄</li>
                          </ul>
                        `
                    }
                }
            })
        </script>
    </body>
</html>

4诫硕、prop

prop 是父組件用來傳遞數(shù)據(jù)的一個自定義屬性,父組件的數(shù)據(jù)需要通過 props 把數(shù)據(jù)傳給子組件。
例:
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <div id="app">
            <my-content></my-content>
        </div>
        <script src="js/vue.js"></script>
        <script>
            Vue.component("my-content",{
                template:`
                  <div>
                     <h2>我是my-content組件的標題</h2>
                     <my-child v-bind:message="msg"></my-child>
                  </div>
                `,
                data:function(){
                    return{
                        msg:"jkdsjoisajdi"
                    
                    }
                }
            })
            
            
            Vue.component("my-child",{
                props:["message"],
                template:`
                    <div>
                      <h3>我是my-child組件中的標題</h3>
                      <p>{{message}}</p>
                    </div>
                `               
            })
            new Vue({
                el:"#app"
            })
        </script>
    </body>
</html>

5刊侯、傳值

組件之間的傳值分為:父級傳子級(用屬性傳值)章办、子級傳父級(用事件傳值)、同級之間傳值滨彻。
父級傳子級例1:
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <div id="app">
            <my-content></my-content>
        </div>
        <script src="js/vue.js"></script>
        <script>
            Vue.component("my-content",{
                template:`
                  <div>
                     <h2>我是my-content組件的標題</h2>
                     <my-child v-bind:message="msg"></my-child>
                  </div>
                `,
                data:function(){
                    return{
                        msg:"hello Vue"
                    
                    }
                }
            })
            
            
            Vue.component("my-child",{
                props:["message"],
                template:`
                    <div>
                      <h3>我是my-child組件中的標題</h3>
                      <p>{{message}}</p>
                    </div>
                `               
            })
            new Vue({
                el:"#app"
            })
        </script>
    </body>
</html>

例2:
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <div id="app">
            <my-father></my-father>
        </div>
        <script src="js/vue.js"></script>
        <script>
            Vue.component("my-father",{
                template:`
                   <div>
                     <my-son v-bind:tit="title"></my-son>
                     <my-list v-bind:fruit="arr"></my-list>
                   </div>
                `,
                data:function(){
                    return{
                        arr:["蘋果","橙子","香蕉","香梨"],
                        title:"水果列表"
                    }
                }
            })
            Vue.component("my-son",{
                props:["tit"],
                template:`
                  <h2>{{tit}}</h2>
                `
            })
            Vue.component("my-list",{
                props:["fruit"],
                template:`
                  <ul>
                    <li v-for="value in fruit">{{value}}</li>
                  </ul>
                `
            })
            new Vue({
                el:"#app"
            })
        </script>
    </body>
</html>

子級傳父級例1:
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <div class="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("send",this.msg)
                    }
                }
            })
            new Vue({
                el:".app"
            })
        </script>
    </body>
</html>

例2:
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <div class="app">
            <my-father></my-father>
        </div>
        <script src="js/vue.js"></script>
        <script>
            Vue.component("my-father",{
                template:`
                  <div>
                     <h1>這是父組件</h1>
                     <p>子組件傳來的數(shù)據(jù)為:{{mess}}</p>                  
                     <my-child @send="revMsg"></my-child>
                  </div>
                `,
                data:function(){
                    return{
                        mess:""
                    }
                },
                methods:{
                    revMsg:function(txt){
                        this.mess=txt
                    }
                }
            })
            Vue.component("my-child",{
                template:`  
                  <div>
                    <h1>這是子組件</h1>
                     <input type="mess" v-model="msg">
                    <button @click="sendFather">向父組件傳遞參數(shù)</button>
                  </div>
                  
                `,
                data:function(){
                    return{
                        msg:""
                    }
                },
                methods:{
                    sendFather:function(){
                        
                        this.$emit("send",this.msg)
                    }
                }
            })
            new Vue({
               el:".app"
            })
        </script>
    </body>
</html>

練習1

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <div class="app">
           <my-father></my-father>
        </div>
        <script src="js/vue.js"></script>
        <script>
            Vue.component("my-father",{
                template:`
                  <div>
                    <input type="text" v-model="txt">
                    <button @click="add">添加</button>
                    <my-list v-bind:fruit="arr"></my-list>
                    
                  </div>
                `,
                data:function(){
                    return{
                        arr:["蘋果","香蕉","梨"]
                        
                    }
                },
                methods:{
                    add:function(){
                        this.arr.push(this.txt),
                        this.txt=""
                    }                   
                }
            })
            
            Vue.component("my-list",{
                props:["fruit"],
                template:`
                  <ul>
                    <li v-for="(value,index) in fruit">{{value}} <button @click="del(index)">刪除 </button></li>
                  </ul>
                `,
                methods:{
                    del:function(ind){
                        this.fruit.splice(ind,1)
                    }
                }
            })
            new Vue({
                el:".app"
                
            })
        </script>
    </body>
</html>

練習2

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <link rel="stylesheet" href="css/bootstrap.css" />
    </head>
    <body>
        <div class="app">
          <my-father></my-father>
        </div>
        <script src="js/vue.js"></script>
        <script>
            Vue.component("my-father",{
                template:`
                   <div>
                     <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:[
                            {name:"apple",price:3,count:3,sub:9},
                            {name:"orange",price:4,count:4,sub:16},
                            {name:"pear",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.name}}</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>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末藕届,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子亭饵,更是在濱河造成了極大的恐慌休偶,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,198評論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件辜羊,死亡現(xiàn)場離奇詭異踏兜,居然都是意外死亡,警方通過查閱死者的電腦和手機八秃,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評論 3 398
  • 文/潘曉璐 我一進店門碱妆,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人昔驱,你說我怎么就攤上這事疹尾。” “怎么了骤肛?”我有些...
    開封第一講書人閱讀 167,643評論 0 360
  • 文/不壞的土叔 我叫張陵纳本,是天一觀的道長。 經(jīng)常有香客問我腋颠,道長繁成,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,495評論 1 296
  • 正文 為了忘掉前任秕豫,我火速辦了婚禮朴艰,結(jié)果婚禮上观蓄,老公的妹妹穿的比我還像新娘。我一直安慰自己祠墅,他們只是感情好侮穿,可當我...
    茶點故事閱讀 68,502評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著毁嗦,像睡著了一般亲茅。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上狗准,一...
    開封第一講書人閱讀 52,156評論 1 308
  • 那天克锣,我揣著相機與錄音,去河邊找鬼腔长。 笑死袭祟,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的捞附。 我是一名探鬼主播巾乳,決...
    沈念sama閱讀 40,743評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼鸟召!你這毒婦竟也來了胆绊?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,659評論 0 276
  • 序言:老撾萬榮一對情侶失蹤欧募,失蹤者是張志新(化名)和其女友劉穎压状,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體跟继,經(jīng)...
    沈念sama閱讀 46,200評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡种冬,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,282評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了还栓。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片碌廓。...
    茶點故事閱讀 40,424評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡传轰,死狀恐怖剩盒,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情慨蛙,我是刑警寧澤辽聊,帶...
    沈念sama閱讀 36,107評論 5 349
  • 正文 年R本政府宣布,位于F島的核電站期贫,受9級特大地震影響跟匆,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜通砍,卻給世界環(huán)境...
    茶點故事閱讀 41,789評論 3 333
  • 文/蒙蒙 一玛臂、第九天 我趴在偏房一處隱蔽的房頂上張望烤蜕。 院中可真熱鬧,春花似錦迹冤、人聲如沸讽营。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽橱鹏。三九已至堪藐,卻和暖如春莉兰,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背礁竞。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評論 1 271
  • 我被黑心中介騙來泰國打工糖荒, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人模捂。 一個月前我還...
    沈念sama閱讀 48,798評論 3 376
  • 正文 我出身青樓寂嘉,卻偏偏與公主長得像,于是被迫代替她去往敵國和親枫绅。 傳聞我的和親對象是個殘疾皇子泉孩,可洞房花燭夜當晚...
    茶點故事閱讀 45,435評論 2 359

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

  • 前言 您將在本文當中了解到,往網(wǎng)頁中添加數(shù)據(jù),從傳統(tǒng)的dom操作過渡到數(shù)據(jù)層操作,實現(xiàn)同一個目標,兩種不同的方式....
    itclanCoder閱讀 25,816評論 1 12
  • 什么是組件? 組件 (Component) 是 Vue.js 最強大的功能之一并淋。組件可以擴展 HTML 元素寓搬,封裝...
    youins閱讀 9,487評論 0 13
  • 此文基于官方文檔,里面部分例子有改動县耽,加上了一些自己的理解 什么是組件句喷? 組件(Component)是 Vue.j...
    陸志均閱讀 3,833評論 5 14
  • 作為一個合格的開發(fā)者,不要只滿足于編寫了可以運行的代碼兔毙。而要了解代碼背后的工作原理唾琼;不要只滿足于自己的程序...
    六個周閱讀 8,451評論 1 33
  • 組件(Component)是Vue.js最核心的功能,也是整個架構(gòu)設(shè)計最精彩的地方澎剥,當然也是最難掌握的锡溯。...
    六個周閱讀 5,619評論 0 32