组件

作者: 大宝贝_4c6e | 来源:发表于2018-09-20 15:10 被阅读0次

    1、什么是组件?

    组件(component)是Vue最强大的功能之一,组件化开发,组件可以扩展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 是父组件用来传递数据的一个自定义属性,父组件的数据需要通过 props 把数据传给子组件。
    例:
    <!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>子组件传来的数据为:{{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">向父组件传递参数</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">数量</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>
    
    

    相关文章

      网友评论

          本文标题:组件

          本文链接:https://www.haomeiwen.com/subject/itqnnftx.html