Vue组件

作者: e8faf1ff57ad | 来源:发表于2018-09-21 14:43 被阅读0次

1.组件:是vue最强大的功能之一,组件化开发,可以扩展HTML元素,封装可重用代码。(分为全局和局部)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <div id="name">
            <my></my>
        </div>
        <script src="js/vue.js"></script>
        <script>
            Vue.component("my",{
                template:`
                  <div>
                  <h1>fdsfg</h1>
                  <button @click="alt">anniu</button>
                  </div>
                `
            ,
            data:function(){
                return{
                    msg:"gdshjfgsjg"
                }
            },
            methods:{
                alt:function(){
                    alert:(fdsffg)
                }
            }
            })
            new Vue({
                el:"#name",
            })
        </script>
    </body>
</html>

2.组件之间的传值

父传子:用属性传

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

子传父:用事件传

<!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>
                   <h1>{{mess}}</h1>
                    <my-child v-bind:send='list'></my-child>
                   <input type="text" v-model="msg"/>
                   
                 </div>
                `,
                data:function(){
                    return{
                        mess:"guhg"
                    }
                }               
            })
            Vue.component("my-child",{
                template:`
                 <button @click="sendFather">添加<button>
                `,
                data:function(){
                    return{
                        msg:""
                    }
                },
                methods:{
                    sendFather:function(){                      
                        this.$emit("push",this.msg)
                    }
                }
            })
            new Vue({
                el:"#app"
            })
        </script> 
    </body>
</html>

3.实例
水果列表

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
  <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>
</body>
</html>

添加

<!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>
                    <input type="text" v-model="txt"/>
                    <button @click="add">添加</button>
                       <my-list v-bind:fruit="arr"></my-list>
                    </div>
                `
                ,
                data:function(){
                    return{
                        arr:[1,2,3,4]
                        
                    }
                },
                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>

购物车

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
    </head>
    <body>
        <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>
                    <td colspan=5>总价:{{sum}}</td>
                    </tr>
                  </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>
S

相关文章

网友评论

      本文标题:Vue组件

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