Vue组件

2018-09-21  本文已影响0人  七缘灬

Vue组件是Vue.js最强大的功能之一
作用:组件可以扩展html元素,封装可重用的代码。
组件之间的传值:1.父传子 2.子传父 3.同级之间传值

1.组件

<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>
    <script src='js/vue.js'></script>
    <script>
        //全局:
        Vue.component('my-component',{
            template:`
             <div>
              <h1>这是一个组件</h1>
              <ul>
               <li>1111</li>
               <li>1111</li>
               <li>1111</li>
               <li>1111</li>
              </ul>
           </div>     
            `
        })
        
        new Vue({
            el:'#app',
                components:{
                'my-component':{
                 template:``
             }
            }
        })
    
    </script>

2.例子:点击出现弹出框

 <div id='app'>
       <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>

效果如下:


提示框.png

3.例子:父传子练习

<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:'dgddbghfghfnh'
                }
            }
       }) 
       
       Vue.component("my-child",{
           props:['message'],
           template:`
              <div>
               <h3>我是my-child组件中的标题</h3>
               <p>{{message}}</p>
              </div>
            `
       })
       new Vue({
          el:'#app'
       })
    
    </script>

效果如下:


父传子.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>

效果如下:


水果列表.png

4.例子:组件添加删除

<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 @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)
            }
        }
    })   
     
    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)
            }
        }
    })
       
     new Vue({
         el:'#app'
     }) 
   </script>

效果如下:


添加删除.png

5.例子:组件购物车

<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'>数量</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>

效果如下:


购物车.png

6.组件子传父

<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:'我们是共产主义接班人'
                }
            },
            methods:{
                sendFather:function(){
                   this.$emit('自定义事件',参数) 
                    this.$emit('send',this.msg)
                }
            }
        })
        
        
        new Vue({
            el:"#app"
        })
    </script>

效果如下:


子传父.png

7.子组件向父组件传值

<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>

效果如下:


子组件.png
上一篇 下一篇

猜你喜欢

热点阅读