子组件向父组件传递数据

$emit

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
</head>

<body>
    <div id="demo">
        {{total}}
            <!-- 自定义组件进行传递数据 -->
        <child-assemly @change='handleCount'></child-assemly>
    </div>

    <script>
        Vue.component('child-assemly',{
            template:'<div>\
                      <button @click="handleadd">+1000 //子组件中定义了点击事件 事件定义在子组件的methods中</button>\
                      <button @click="handleminus">-1000 //子组件中定义了点击事件</button>\
                      </div>',
            data:function(){
                return{
                    count:1000
                }
            },
            methods:{
                //template中DOM中的计算方法进行定义
                handleadd:function(){
                    this.count=this.count+1000
                    //$emit('事件名',传递的数据,可以传递多个参数)  change就是父组件中DOM中的change 名字随便写
                    this.$emit('change',this.count)
                },
                handleminus:function(){
                    this.count=this.count-1000
                    this.$emit('change',this.count)

                }
            }          
        })
        var app = new Vue({
            el: '#demo',
            data:{
                total:1000
            },
            methods:{
                //自定义组件 value 就是子组件中传递出来的数据  this.count
                handleCount:function(value){
                        this.total=value
                }
            }
        }) 
    </script>
</body>

</html>
复制代码

转载于:https://juejin.im/post/5ba0d15ee51d451a3f4bf71f

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值