Vue中data返回对象和返回值的区别

速记:粗浅的理解是,事件的结果是影响单个组件还是多个组件。因为大部分组件是要共享的,但他们的data是私有的,所以每个组件都要return一个新的data对象

返回对象的时候

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title></title>
    <link rel="stylesheet" href="">
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
    <div id="components-demo3" class="demo">
        <button-counter2></button-counter2>
        <button-counter2></button-counter2>
        <button-counter2></button-counter2>
    </div>
    <script>
    Vue.component('button-counter2',{
        data:function(){
            return {
                count:0
            }
        },
        template:'<button v-on:click="count++">点击了{{count}}次</button>'
    })
    
    new Vue({
        el:'#components-demo3'
    })
    
    </script>
</body>
</html>

1037363-20190313112332335-1076964043.png
data直接返回值的时候

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title></title>
    <link rel="stylesheet" href="">
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
    <div id="components-demo3" class="demo">
        <button-counter2></button-counter2>
        <button-counter2></button-counter2>
        <button-counter2></button-counter2>
    </div>
    <script>
    buttonCounter2Data={
        count:0
    }
    Vue.component('button-counter2',{
        data:function(){
            return buttonCounter2Data
        },
        template:'<button v-on:click="count++">点击了{{count}}次</button>'
    })
    
    new Vue({
        el:'#components-demo3'
    })
    
    </script>
</body>
</html>

1037363-20190313112443730-104032439.png

转载于:https://www.cnblogs.com/smart-girl/p/10521950.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值