Vue 組件

組件

組件 component:
組件(Component)是 Vue.js 最強(qiáng)大的功能之一。組件可以擴(kuò)展 HTML 元素跟压,封裝可重用的代碼鸣哀。
組件可被反復(fù)調(diào)用或使用
全局組件

Vue.component('組件名’,{
        template:` `
})

局部組件

 new Vue({
        el:"#app",
        components:{
           '組件名':{
                template:` `
            }
        }
    })

例:

div部分

<div id="app">
    <my-component></my-component>
    <my-component></my-component>
    <my-component></my-component>
    <my-component></my-component>
    <my-component></my-component>
    <ul>
        <li></li>
    </ul>
</div>

JS部分

<script src="../js/vue.js"></script>
<script>
    //全局:
    Vue.component('my-component',{
        template:`
              <ul>
               <li>aaaa</li>
              </ul>

            `
    })

    new Vue({
        el:'#app',
        //局部
      /*  components:{
            'my-component':{
                template:``
            }
        }*/
    })

</script>

1.組件中點(diǎn)擊按鈕彈出警示框

<div id='app'>
    <!--       <p>{{msg}}</p>-->
    <my-component></my-component>

</div>
<script src="../js/vue.js"></script>
<script>
    Vue.component("my-component",{
        template:`
                <div>
                    <h1>{{msg}}</h1>
                    <button @click='alt'>按鈕</button>
                </div>
              `,
        data:function(){
            return{
                msg:'dcgf'
            }
        },
        methods:{
            alt:function(){
                alert(11111)
            }
        }
    })
    new Vue({
        el:'#app',

    })
</script>

效果如下:
Image 1.png

2.組件的嵌套

組件之間的傳值
父?jìng)髯樱河脤傩詡?/p>

props:["屬性"]

子傳父:用事件傳

$emit:this.$emit('自定義事件','參數(shù)')

同級(jí)之間傳值/非父子之間傳值:

借助第三方量 var bus=new Vue() bus.$emit('send',msg=>{})

父?jìng)髯?/h1>

例:

<div id='app'>
    <my-content></my-content>

</div>
<script src='../js/vue.js'></script>
<script>
    Vue.component("my-content",{

        template:`
              <div>
                 <h2>我是my-content組件的標(biāo)題</h2>
                 <my-child v-bind:message='msg'></my-child>

             </div>
            `,
        data:function(){
            return{
                msg:'dgddbghfghfnh'
            }
        }
    })

    Vue.component("my-child",{
        props:['message'],
        template:`
              <div>
               <h3>我是my-child組件中的標(biāo)題</h3>
               <p>{{message}}</p>
              </div>
            `
    })
    new Vue({
        el:'#app'
    })

</script>

效果如下:


Image 1.png

例:

<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:['apple','pear','orange'],
                title:'水果列表'
        }
    }
    }),
    //title
    Vue.component("my-son",{
        props:['tit'],
        template:`
            <h2>{{tit}}</h2>
            `
    }),
        //arr
            Vue.component('my-list',{
                props:['fruit'],
                template:`
                  <ul>
                      <li v-for="value in fruit">{{value}}</li>
                 </ul>
            `
            }),


    new Vue({
        el:"#app",
    })
</script>

效果如下:


Image 2.png

3.水果列表

<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="fruit">
                <button v-on:click="add">添加</button>
                <my-child v-bind:fruList="list"></my-child>
            </div>


            `,
        data:function(){
            return{
                list:["apple","pear","orange"],
                fruit:""
            }
        },
        methods:{
            add:function(){
                this.list.push(this.fruit)
                this.fruit=""
            }
        }

    }),
            Vue.component("my-child",{
                props:['fruList'],
                template:`
                    <ul>
                  <li v-for="(value,index) in fruList">
                       {{value}}
                        <button @click='delt(index)'>刪除</button>
                   </li>
                </ul>

            `,
                methods:{
                    delt:function(ind){
                        this.fruList.splice(ind,1)
                        this.fruit=""
                    }
                }

            }),
            new Vue({
                el:"#app",
            })
</script>

效果如下:


Image 3.png

4.shopping

<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'>編號(hào)</th>
                       <th class='text-center'>名稱</th>
                       <th class='text-center'>單價(jià)</th>
                       <th class='text-center'>數(shù)量</th>
                       <th class='text-center'>小計(jì)</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>總價(jià):¥{{sum}}</td>
                 </tr>
             </tbody>

          `,
                data:function(){
                    return{
                        sum:0
                    }
                },
                methods:{
                    add:function(ind){
                        this.list[ind].count++ ;
                        //計(jì)算小計(jì)
                        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--
                        }
                        //計(jì)算小計(jì)
                        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>

效果如下:


Image 4.png

子傳父

例:

<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:'JSS是共產(chǎn)主義接班人'
                    }
                },
                methods:{
                    sendFather:function(){
//                   this.$emit('自定義事件',參數(shù))
                        this.$emit('send',this.msg)
                    }
                }

            }),
    new Vue({
        el:"#app",
    })
</script>

效果如下:


Image 5.png

5.子傳父

<div id="app">
    <my-father></my-father>
</div>
<script src="../js/vue.js"></script>
<script>
    Vue.component("my-father",{
        template:`
            <div>
                <h1>這個(gè)是父組件</h1>
                <span>請(qǐng)?jiān)诟附M件中添加數(shù)據(jù)</span>
                <span>{{mess}}</span>
                <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>這個(gè)是子組件</h1>
                <input type="text" v-model="msg">
                <button @click='sendFather'>給父組件</button>
                </div>
            `,
                data:function(){
                    return{
                        msg:"",
                        text:""

                    }
                },
                methods:{
                    sendFather:function(){
                        this.$emit('send',this.msg)
                        this.msg=this.text



                    }
                }

            }),
    new Vue({
        el:"#app",
    })
</script>

效果如下:


Image 1.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末季希,一起剝皮案震驚了整個(gè)濱河市恨樟,隨后出現(xiàn)的幾起案子半醉,更是在濱河造成了極大的恐慌,老刑警劉巖劝术,帶你破解...
    沈念sama閱讀 219,188評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件缩多,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡养晋,警方通過查閱死者的電腦和手機(jī)瞧壮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來匙握,“玉大人,你說我怎么就攤上這事陈轿∪Ψ模” “怎么了?”我有些...
    開封第一講書人閱讀 165,562評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵麦射,是天一觀的道長(zhǎng)蛾娶。 經(jīng)常有香客問我,道長(zhǎng)潜秋,這世上最難降的妖魔是什么蛔琅? 我笑而不...
    開封第一講書人閱讀 58,893評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮峻呛,結(jié)果婚禮上罗售,老公的妹妹穿的比我還像新娘辜窑。我一直安慰自己,他們只是感情好寨躁,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,917評(píng)論 6 392
  • 文/花漫 我一把揭開白布穆碎。 她就那樣靜靜地躺著,像睡著了一般职恳。 火紅的嫁衣襯著肌膚如雪所禀。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,708評(píng)論 1 305
  • 那天放钦,我揣著相機(jī)與錄音色徘,去河邊找鬼。 笑死操禀,一個(gè)胖子當(dāng)著我的面吹牛褂策,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播床蜘,決...
    沈念sama閱讀 40,430評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼辙培,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了邢锯?” 一聲冷哼從身側(cè)響起扬蕊,我...
    開封第一講書人閱讀 39,342評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎丹擎,沒想到半個(gè)月后尾抑,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,801評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蒂培,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,976評(píng)論 3 337
  • 正文 我和宋清朗相戀三年再愈,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片护戳。...
    茶點(diǎn)故事閱讀 40,115評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡翎冲,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出媳荒,到底是詐尸還是另有隱情抗悍,我是刑警寧澤,帶...
    沈念sama閱讀 35,804評(píng)論 5 346
  • 正文 年R本政府宣布钳枕,位于F島的核電站缴渊,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏鱼炒。R本人自食惡果不足惜衔沼,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,458評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧指蚁,春花似錦菩佑、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至缘圈,卻和暖如春劣光,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背糟把。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工绢涡, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人遣疯。 一個(gè)月前我還...
    沈念sama閱讀 48,365評(píng)論 3 373
  • 正文 我出身青樓雄可,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親缠犀。 傳聞我的和親對(duì)象是個(gè)殘疾皇子数苫,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,055評(píng)論 2 355

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

  • 此文基于官方文檔,里面部分例子有改動(dòng)辨液,加上了一些自己的理解 什么是組件虐急? 組件(Component)是 Vue.j...
    陸志均閱讀 3,828評(píng)論 5 14
  • 在這一節(jié)里,我們將會(huì)了解到Vue的組件滔迈,理解組件是如何工作的止吁,并利用一系列的例子證明,用組件化的思想開發(fā)項(xiàng)目燎悍,會(huì)給...
    嘉寶_Appian閱讀 3,273評(píng)論 6 17
  • 推薦我的vue教程:VUE系列教程目錄 上篇講解了vue-router路由入門敬惦,現(xiàn)在講講關(guān)于vue組件的內(nèi)容。如果...
    儂姝沁兒閱讀 2,359評(píng)論 6 20
  • 1. 組件 a. 什么是組件 組件化開發(fā) 我們可以很直觀的把一個(gè)復(fù)雜的頁(yè)面分割成若干個(gè)獨(dú)立的組件谈山,每個(gè)組件包含...
    goodluckall閱讀 139評(píng)論 0 0
  • 組件注冊(cè) 組件名 在注冊(cè)一個(gè)組件的時(shí)候俄删,我們始終需要給它一個(gè)名字。 該組件名就是Vue.component的第一個(gè)...
    oWSQo閱讀 399評(píng)論 0 1