zoukankan      html  css  js  c++  java
  • 父子组件的数据和事件传递

    当在vue中定义一个子组件,从父组件给子组件传递数据可以通过在子经组件定义中通过props属性来传递数据过去:

    <div id="box">
            <cart :goods="goods"></cart>
            <span>总价:{{totalPrice}}元</span>
        </div>
    
        <script type="text/x-template" id="cart">
            <table border="1px" width="90%">
                    <tr>
                    <th>序号</th>
                    <th>产品名称</th>
                    <th>价格</th>
                    <th>数量</th>
                    </tr>
    
                    <tr v-for="(item,key) in goods">
                        <td>{{key + 1}}</td>
                        <td>{{item.goodsName}}</td>
                        <td>{{item.price}}</td>
                        <td>
                        <input type="text"  v-model="item.num" @blur="sum">
                        </td>
                    </tr>
                    </table>
        </script>
    
        <script type="text/javascript">
    
            var cart = {
                props:['goods'],
                template:'#cart',
            };
    
           var app = new Vue({
               el:'#box',
               mounted(){
                   this.total();
               },
               methods:{
                   total(){
                       console.log('total');
                       this.totalPrice = 0;
                       this.goods.forEach((v)=>{
                           this.totalPrice += v.num * v.price;
                       })
                   }
               },
               data:{
                   totalPrice:0,
                   goods:[
                       {goodsName:'iphone7Plus',price:600,num:1},
                       {goodsName:'vip卡',price:200,num:1},
                   ]
               },
               components:{cart}
           });
        </script>
    

      然后标签页中使用:goods来进行绑定数据,然后就可以在模板中使用数据了,效果如下:

    如果在子组件中改变数量值时,如果修改总价,此时就牵涉到了从子组件发送事件到父组件,此时就得使用$emit函数进行触发:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
        <link href="https://cdn.bootcss.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet">
        <script type="text/javascript" src="./node_modules/vue/dist/vue.js"></script>
        <script type="text/javascript" src="./node_modules/vue-resource/dist/vue-resource.js"></script>
    </head>
    <body>
        <div id="box">
            <cart :goods="goods" @sum="total"></cart>
            <span>总价:{{totalPrice}}元</span>
        </div>
    
        <script type="text/x-template" id="cart">
            <table border="1px" width="90%">
                    <tr>
                    <th>序号</th>
                    <th>产品名称</th>
                    <th>价格</th>
                    <th>数量</th>
                    </tr>
    
                    <tr v-for="(item,key) in goods">
                        <td>{{key + 1}}</td>
                        <td>{{item.goodsName}}</td>
                        <td>{{item.price}}</td>
                        <td>
                        <input type="text"  v-model="item.num" @blur="sum">
                        </td>
                    </tr>
                    </table>
        </script>
    
        <script type="text/javascript">
    
            var cart = {
                props:['goods'],
                template:'#cart',
                methods:{
                    sum(){
                        console.log('sum');
                        this.$emit('sum')
                    }
                }
            };
    
           var app = new Vue({
               el:'#box',
               mounted(){
                   this.total();
               },
               methods:{
                   total(){
                       console.log('total');
                       this.totalPrice = 0;
                       this.goods.forEach((v)=>{
                           this.totalPrice += v.num * v.price;
                       })
                   }
               },
               data:{
                   totalPrice:0,
                   goods:[
                       {goodsName:'iphone7Plus',price:600,num:1},
                       {goodsName:'vip卡',price:200,num:1},
                   ]
               },
               components:{cart}
           });
        </script>
    </body>
    </html>
    

      

  • 相关阅读:
    Luogu P1090 合并果子(优先队列 || priority_queue)
    Luogu P1012 拼数
    hibernate5.2的基本配置
    [bzoj1210][HNOI2004]邮递员【插头dp】
    [bzoj3470]Freda’s Walk【概率与期望dp】
    [bzoj4851][Jsoi2016]位运算【矩阵乘法】【状压dp】
    [bzoj4852][Jsoi2016]炸弹攻击【随机化】
    [bzoj4853][Jsoi2016]飞机调度【最短路】【网络流】
    [bzoj4850][Jsoi2016]灯塔【暴力】
    [bzoj4919][Lydsy1706月赛]大根堆【dp】【启发式合并】【stl】
  • 原文地址:https://www.cnblogs.com/wntd/p/9012385.html
Copyright © 2011-2022 走看看