computed:用于data中属性的计算。
computed的简单应用
<div id="app">
<!--直接拼接-->
<h2>{{"("+x+","+y+")"}}</h2>
<!--computed-->
<h2>坐标: {{ xy }}</h2>
<!--methods-->
<h2>坐标: {{ getxy() }}</h2>
</div>
<script>
const app = new Vue({
el:"#app",
data:{
x:6,
y:7
},
computed:{
xy : function () {
return "("+this.x+","+this.y+")"
}
},
methods:{
getxy(){
return "("+this.x+","+this.y+")"
}
}
})
</script>
使用computed而不使用methods的原因是 当页面多次调用时computed且属性不变时只会计算一次,而methods每次调用都会计算