计算属性
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id="app"> <table border="1"> <thead> <th>学科</th> <th>分数</th> </thead> <tbody> <tr> <td>物理</td> <td><input type="text" v-model.number="physics"></td> </tr> <tr> <td>数学</td> <td><input type="text" v-model.number="math"></td> </tr> <tr> <td>英语</td> <td><input type="text" v-model.number="english"></td> </tr> <tr> <td>总分</td> <td>{{sum}}</td> </tr> <tr> <td>平均分</td> <td>{{avg}}</td> </tr> </tbody> </table> </div> <script src="../lib/vue.js"></script> <script src="js/main.js"></script> </body> </html>
var app = new Vue({
el:"#app",
data:{
math:90,
physics:80,
english:70,
},
computed:{
sum:function () {
return this.math+this.physics+this.english
},
avg:function () {
return Math.round(this.sum/3)
}
}
})
本文来自博客园,作者:一石数字欠我15w!!!,转载请注明原文链接:https://www.cnblogs.com/52-qq/p/8386314.html