2018-09-24

计算属性(computed)用于处理复杂逻辑

computed:{
}

computed做为vue的选项是固定的
例子:

<div id="itany">
    <p>{{mes}}</p>
    <p>{{count}}</p>
</div>
<script src="../js/vue.js"></script>
<script>
    new Vue({
        el:'#itany',
        data:{
            mes:'hello vue'
        },
        computed:{
            count:function(){
                                //切割       翻转      拼接
                return this.mes.split(' ').reverse().join('---')
            }

        }
    })

</script>

输出结果为:
hello vue
vue---hello

练习

要求:点击button按钮使数字以对应的价格改变

13921236-6bacda96198b46a1.png

on v-on:click="num">总和</button>
<p>{{arr}}</p>
</div>
<script src="../js/vue.js"></script>
<script>
new Vue({
el:'#itany',
data:{
name:{price:2,count:0},
names:{price:4,count:0},
see:true
},
methods:{
num:function(){
this.name.count++,
this.names.count++
}
},
computed:{
arr:function(){
return this.name.pricethis.name.count+this.names.pricethis.names.count
}
}
})
</script>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容