2018-09-19 組件

組件:

組件是vue最強(qiáng)大的功能之一泪酱,組件化開發(fā)钓觉,
組件可以擴(kuò)展html元素茴肥,封裝可重用的代碼。
組件之間的傳值:①:父傳子 用屬性傳 荡灾;②:子傳父:用事件傳瓤狐;③:同級之間傳值

組件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div id="itany">
        <name></name>
        <names></names>
    </div>
    <script src="vue.js"></script>
    <script>
        //全局
        /*Vue.component('name',{
            template:`
                <ul>
                    <li>aaaaaa</li>
                    <li>bbbbbb</li>
                    <li>cccccc</li>
                </ul>
            `
        })*/
        
        //局部
        new Vue({
            el:'#itany',
            components:{
                'names':{
                template:`
                    <ul>
                        <li>111111</li>
                        <li>222222</li>
                        <li>333333</li>
                    </ul>
                `
            }
            }
        })
    </script>
</body>
</html>

屏幕展示:
1.png

點(diǎn)擊出現(xiàn)彈出框

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div id="it">
        <name></name>
    </div>
    <script src="vue.js"></script>
    <script>
        Vue.component('name',{
            template:`
                <div>
                    <h1>{{msg}}</h1>
                    <button @click='alt'>點(diǎn)擊</button>
                </div> 
            `,
            data:function(){
                return{
                    msg:'asd'
                }
            },
            methods:{
                alt:function(){
                    alert(123)
                }
            }
        })
            new Vue({
            el:'#it'
        })
    </script>
</body>
</html>

屏幕展示:
2.png

組件傳值

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div id="it">
        <one></one>
        <two></two>
    </div>
    <script src="vue.js"></script>
    <script>
        Vue.component('one',{
            template:`
                <div>
                    <h1>我是老大</h1>
                    <two v-bind:baby=msg></two>
                </div>
            `,
            data:function(){
            return{
                msg:'hahahaha'
            }
        }
    })
        
        Vue.component('two',{
            props:['baby'],
            template:`
                <div>
                    <h3>我是老二</h3>
                    <p>{{baby}}</p>
                </div>
            `
        })
        
        new Vue({
            el:'#it'
        })
    </script>
</body>
</html>

屏幕展示:
3.png
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div id="it">
        <one></one>
    </div>
    <script src="vue.js"></script>
    <script>
        Vue.component('one',{
            template:`
                <div>
                    <two v-bind:a=title></two>
                    <three v-bind:b=arr></three>
                </div>
            `,
            data:function(){
                return{
                    arr:['dog','cat','pig'],
                    title:"動物"
                }
            }
        })
        
        Vue.component('two',{
            props:['a'],
            template:`
                <h2>{{a}}</h2>
            `
        })
        
        Vue.component('three',{
            props:['b'],
            template:`
                <ul>
                    <li v-for='value in b'>{{value}}</li>
                </ul>
            `
        })
        
        new Vue({
            el:'#it'
        })
    </script>
</body>
</html>

屏幕展示:
1.png

點(diǎn)擊添加

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div id="it">
        <one></one>
        
    </div>
    <script src="vue.js"></script>
    <script>
        Vue.component('one',{
            template:`
                <div>
                    <input v-model='txt'>
                    <button @click='a'>添加</button>
                    <two v-bind:tian='arr'></two>
                </div>
            `,
            data:function(){
                return{
                    arr:['吃飯','睡覺','打游戲'],
                     txt:'' 
                }
            },
            methods:{
                a:function(){
                    this.arr.push(this.txt)
                    this.txt=""
                }
            }
        })
        
        Vue.component('two',{
            props:['tian'],
            template:`
                  <ul>
                        <li v-for='value in tian'>{{value}}</li>
                    </ul> 
            `
        
        })
            
        new Vue({
            el:'#it'
        })
    </script>
</body>
</html>

屏幕展示:
2.png
3.png

9.20

添加刪除

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div id="it">
        <one></one>
    </div>
    <script src="vue.js"></script>
    <script>
        Vue.component('one',{
            template:`
                <div>
                    <input v-model='m'>
                    <button @click='alt'>添加</button>
                    <two v-bind:tian=arr></two>
                </div>
            `,
            data:function(){
                return{
                    arr:['逛街','吃飯','看電影'],
                    m:''
                }
            },
            methods:{
                alt:function(){
                    this.arr.push(this.m)
                },
            }
        })
        
        Vue.component('two',{
            props:['tian'],
            template:`
                <ul>
                    <li v-for='(value,index) in tian'>{{value}}
                        <button @click='add(index)'>刪除</button></li>
                </ul>
            `,
            methods:{
                add:function(e){
                    this.tian.splice(e,1)
                }
            }
        })
            
        new Vue({
            el:'#it'
        })
    </script>
</body>
</html>

屏幕展示:

點(diǎn)擊按鈕添加或刪除
4.png

購物車

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel='stylesheet' href="bootstrap.css">
</head>
<body>
    <div id="app">
        <one></one>
              
    </div>
    <script src="vue.js"></script>
    <script>
        Vue.component('one',{
            template:`
                <div class="containter">
                    <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'>總計(jì)</th>
                            </tr>
                        </thead>
                        <two v-bind:list='Flist'></two>
                    </table>
                </div>
            `,
            data:function(){
                return{
                    Flist:[
                        {name:'apple',price:1,count:2,sum:2},
                        {name:'banana',price:2,count:3,sum:6},
                        {name:'orange',price:3,count:4,sum:12}
                    ]
                }
            }
        })
        
        Vue.component('two',{
            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='redu(index)'>-</button>
                            <span>{{value.count}}</span>
                            <button @click='add(index)'>+</button>
                        </td>
                        <td>{{value.sum}}</td>
                    </tr>
                    <tr>
                        <td colspan=5>總價:{{suu}}</td>
                    </tr>
                </tbody> 
            `,
            data:function(){
                return{
                    suu:0
                }
            },
            methods:{
                redu:function(ind){
                    if(this.list[ind].count>1){
                    this.list[ind].count--;
                    }
                    this.list[ind].sum=this.list[ind].price*this.list[ind].count;
                    this.countsuu();
                },
                add:function(ind){
                    this.list[ind].count++;
                    this.list[ind].sum=this.list[ind].price*this.list[ind].count;
                    this.countsuu();
                },
                countsuu:function(){
                    for(var i=0,total=0;i<this.list.length;i++){
                        total+=this.list[i].sum;
                    }
                    this.suu=total;
                }
            }
        })
        
        new Vue({
            el:'#app'
        })
    </script>
</body>
</html>

屏幕展示:
5.png

組件子傳父

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <one></one>
    </div>
    <script src="vue.js"></script>
    <script>
        Vue.component('one',{
            template:`
                <div>
                    <h1>{{mess}}</h1>
                    <two @send='b'></two>
                </div>
            `,
            data:function(){
                return{
                    mess:''
                }
            },
            methods:{
                b:function(mess1){
                    this.mess=mess1
                }
            }
        })
        
        Vue.component('two',{
            template:`
                <button @click='a'>點(diǎn)擊出現(xiàn)</button>
            `,
            data:function(){
                return{
                    msg:'子傳父'
                }
            },
            methods:{
                a:function(){
                    this.$emit('send',this.msg)
                }
            }
        })
        
        new Vue({
            el:'#app'
        })
    </script>
</body>
</html>

屏幕展示:

點(diǎn)擊按鈕出現(xiàn)
7.png

6.png

子傳父點(diǎn)擊添加

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <one></one>
    </div>
    <script src='vue.js'></script>
    <script>
        Vue.component('one',{
            template:`
                <div>
                    <h2>{{as}}</h2>
                    <h1>{{asd}}</h1>
                    <two @send="qwe"></two>
                </div>
            `,
            data:function(){
                return{
                    as:'sss',
                    asd:""
                }
            },
            methods:{
                qwe:function(ind){
                    this.asd=ind
                }
            }
        })
        
        Vue.component('two',{
            template:`
                <div>
                    
                    <input type='text' v-model='txt'> 
                    <button @click='a'>點(diǎn)擊</button>
                </div>
            `,
            data:function(){
                return{
                    msg:'',
                    txt:''
                }
            },
            methods:{
                a:function(){
                    this.msg=this.txt
                    this.$emit('send',this.txt)
                }
            }
        })
        
        new Vue({
            el:'#app'
        })
    </script>
</body>
</html>

屏幕展示:

輸入點(diǎn)擊添加
8.png
9.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市批幌,隨后出現(xiàn)的幾起案子础锐,更是在濱河造成了極大的恐慌,老刑警劉巖荧缘,帶你破解...
    沈念sama閱讀 218,204評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件皆警,死亡現(xiàn)場離奇詭異,居然都是意外死亡截粗,警方通過查閱死者的電腦和手機(jī)信姓,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來绸罗,“玉大人递胧,你說我怎么就攤上這事暑认『杀铮” “怎么了淑掌?”我有些...
    開封第一講書人閱讀 164,548評論 0 354
  • 文/不壞的土叔 我叫張陵豁遭,是天一觀的道長贱鄙。 經(jīng)常有香客問我而晒,道長搪桂,這世上最難降的妖魔是什么略步? 我笑而不...
    開封第一講書人閱讀 58,657評論 1 293
  • 正文 為了忘掉前任描扯,我火速辦了婚禮,結(jié)果婚禮上趟薄,老公的妹妹穿的比我還像新娘绽诚。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,689評論 6 392
  • 文/花漫 我一把揭開白布恩够。 她就那樣靜靜地躺著卒落,像睡著了一般。 火紅的嫁衣襯著肌膚如雪蜂桶。 梳的紋絲不亂的頭發(fā)上儡毕,一...
    開封第一講書人閱讀 51,554評論 1 305
  • 那天,我揣著相機(jī)與錄音扑媚,去河邊找鬼腰湾。 笑死,一個胖子當(dāng)著我的面吹牛疆股,可吹牛的內(nèi)容都是我干的费坊。 我是一名探鬼主播,決...
    沈念sama閱讀 40,302評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼旬痹,長吁一口氣:“原來是場噩夢啊……” “哼附井!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起两残,我...
    開封第一講書人閱讀 39,216評論 0 276
  • 序言:老撾萬榮一對情侶失蹤永毅,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后人弓,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體卷雕,經(jīng)...
    沈念sama閱讀 45,661評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,851評論 3 336
  • 正文 我和宋清朗相戀三年票从,在試婚紗的時候發(fā)現(xiàn)自己被綠了漫雕。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,977評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡峰鄙,死狀恐怖浸间,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情吟榴,我是刑警寧澤魁蒜,帶...
    沈念sama閱讀 35,697評論 5 347
  • 正文 年R本政府宣布,位于F島的核電站吩翻,受9級特大地震影響兜看,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜狭瞎,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,306評論 3 330
  • 文/蒙蒙 一细移、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧熊锭,春花似錦弧轧、人聲如沸雪侥。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽速缨。三九已至,卻和暖如春代乃,著一層夾襖步出監(jiān)牢的瞬間旬牲,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評論 1 270
  • 我被黑心中介騙來泰國打工搁吓, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留引谜,地道東北人。 一個月前我還...
    沈念sama閱讀 48,138評論 3 370
  • 正文 我出身青樓擎浴,卻偏偏與公主長得像员咽,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子贮预,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,927評論 2 355

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