美文网首页
2018-09-19vue组件运用(父给子传)

2018-09-19vue组件运用(父给子传)

作者: 好多好多鱼z | 来源:发表于2018-09-21 19:20 被阅读0次

运用组件添加删除元素(父给子传)

父给子传(用props属性传值)

<!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="inputVal"> <button @click='add'>添加</button>
                   <my-child v-bind:fruit='list'></my-child>
               </div>
             `,
            data:function(){
                return{
                    list:['apple','pear','orange'],
                    inputVal:''
                }
            },
            methods:{
                add:function(){
                   this.list.push(this.inputVal); 
                }
            }
        })
        
        Vue.component('my-child',{
            props:['fruit'],
            template:`
              <ul>
                 <li v-for="(value,index) in fruit">
                    {{value}}
                     <button @click="delt(index)">删除</button>
                  </li>
              </ul>
           `,
            methods:{
                delt:function(ind){
                    this.fruit.splice(ind,1)
                }
            }
        })
        
        
        
        new Vue({
            el:'#app'
        })
    </script>
</body>
</html>
QQ拼音截图未命名.png

运用组件做购物车效果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" 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:product='goods'></my-child>
               </table>
             </div>
            `,
            data:function(){
                return{
                    goods:[
                        {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:['product'],
            template:`
                <tbody>
                  <tr v-for="(value,index) in product">
                     <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:""
                }
            },
            methods:{
                add:function(ind){
                    this.product[ind].count++;
                    //小计、
                    this.product[ind].sub=this.product[ind].count*this.product[ind].price;
                    this.countSum();
                 
                },
                redu:function(ind){
                    if(this.product[ind].count>1){
                       this.product[ind].count--;
                    }
                    //小计
                      this.product[ind].sub=this.product[ind].count*this.product[ind].price;
                     this.countSum();
                },
                countSum:function(){
                    for(var i=0,total=0;i<this.product.length;i++){
                        total+=this.product[i].sub;
                    }
                    this.sum=total;
                }
                
                
            }
        })
        
        
        new Vue({
            el:'#app'
        })
    </script>
</body>
</html>
QQ拼音截图未命名.png

相关文章

  • 2018-09-19vue组件运用(父给子传)

    运用组件添加删除元素(父给子传) 父给子传(用props属性传值) 运用组件做购物车效果

  • Vue_组件间传值

    1、父组件传值给子组件2、子组件传值给父组件 1、父组件传值给子组件 2、子组件传值给父组件

  • vue2.0 父子组件传值

    父组件传值给子组件 子组件 子组件通过 props 接收值 父组件 父组件通过标签属性传值 子组件传值给父组件 子...

  • react子组件、父组件相互传值

    子组件传图片路径imgUrl值给父组件,父组件传imgaddr值给子组件子组件: 父组件:

  • 【Vue3 从入门到实战 进阶式掌握完整知识体系】011-探索组

    2、组件间传值及传值校验 父组件给子组件传值 运行结果 父组件给子组件传动态参数 运行结果 子组件校验父组件的传参...

  • vue2.0小结

    父组件与子组件之间的通信 父组件传值给子组件,通过props 子组件传值给父组件,通过$emit 如果在父组件想在...

  • VUE组件之间传值

    1.父组件传值给子组件 父组件image.png 子组件image.png 2.子组件传值给父组件 方法1: 子组...

  • 02.1 - vue 组件间传参 props

    一、用法 父组件、子组件概念: 二、父组件传参给子组件 - props 1、传静态数据 1、父组件传参 2、子组...

  • 父子组件通信

    父级给子组件传值: 在父级中调用组件 在子组件js中获取 子组件给父级传值 在子组件中 父级 html 父级js

  • uni组件传值注意

    目录介绍 01.组件传值遇到坑 02.父组件传值给子组件 03.子组件传值给父组件 01.组件传值遇到坑 子组件给...

网友评论

      本文标题:2018-09-19vue组件运用(父给子传)

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