计算属性(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按钮使数字以对应的价格改变
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>