<!DOCTYPEhtml><html><head><metacharset="utf-8"><title></title><scriptsrc="../vue.js"type="text/javascript"charset="utf-8"></script></head><body><divid="app"><pv-for="item in res">{{item.name}}:{{item.marks}}</p><p>总分:{{totalmarks}}</p></div><scripttype="text/javascript">var vm =newVue({el:"#app",data:{res:[{name:"english",marks:90},{name:"math",marks:80},{name:"history",marks:70}]},//计算属性computed:{//通过一个方法得出一个数据 方法名就是计算后的数据变量名 方法的返回值就是这个数据的值//计算属性会对依赖到的所有数据进行监听 监听的数据发生变化 计算方法会重新执行 totalmarks:function(){let total =0;for(let i =0; i <this.res.length; i++){
total +=parseInt(this.res[i].marks)}return total
}}})// computed和watch 的区别// 1.computed必须有返回值 watch不一定// 2.watch只监听一个数据 computed 可以监听多个// 3.computed 有缓存</script></body></html>