计算属性本质上就是一个 function 函数,它可以实时监听 data 中数据的变化,并 return 一个计算后的新值。
计算属性关键词: computed。
举个栗子
<template>
<div id="app">
<h2>{{num}}的平方为{{a2}}</h2>
</div>
</template>
<script>
export default {
name: 'App',
data(){
return{
num:13,
}
},
computed:{
a2(){
return this.num*this.num
}
}
}
</script>
<style scoped>
h2{
color: #42b983;
}
</style>