Vue —— 计算属性
-
computed
处理元数据,便于进行二次利用。(比如:消费税自动计算功能)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>caculate_property.html</title>
<script src="../js/vue.js"></script>
</head>
<body>
<div id="example">
<p>衬衫的价格是:{{discount}}元, 但你需要支付{{discount2}}元</p>
</div>
</body>
<script>
new Vue({
el: "#example",
data: {
price: 0.915
},
//自动执行程序
computed: {
//相当于return数据到discount中,然后html中读取了discount的内容
discount: function() {
return this.price * 10;
},
//前面返回的数据可以继续使用
discount2:function() {
return this.discount * 10;
}
}
});
</script>
</html>