计算属性与方法的区别:
- 计算属性是基于它们的依赖进行缓存的
- 方法不存在缓存
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<div>{{sumComputed}}</div>
<div>{{sumComputed}}</div>
<div>{{sum()}}</div>
<div>{{sum()}}</div>
</div>
<script src="../lib/vue.js"></script>
<script type="text/javascript">
const vm= new Vue({
el:"#app",
data:{
num:100
},
methods:{
sum:function () {
console.log("in methods")
let total = 0;
for (let i =0;i<=this.num;i++){
total+=i;
}
return total;
}
},
computed:{
sumComputed:function () {
console.log("in computed")
let total = 0;
for (let i =0;i<=this.num;i++){
total+=i;
}
return total;
}
}
})
</script>
</body>
</html>
对应视频分享:computed