计算属性和侦听器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body >
<div id = "app">
苹果:单价 {{price1}} 购买数量 <input type="number" v-model="num1"> </br>
西瓜:单价 {{price2}} 购买数量 <input type="number" v-model="num2"> </br>
总价:{{totalPrice}} </br>
<h3>{{msg}}</h3>
</div>
<script src="../node_modules/vue/dist/vue.js"></script>
<script>
let ap = new Vue({
el:"#app",//绑定元素
data:{//封装数据
price1:6.5,
price2:3.5,
num1:1,
num2:1,
msg:""
},
methods:{//封装方法
func(){
this.num++;
}
},
computed: {
totalPrice(){
return this.price1*this.num1 + this.price2*this.num2
}
},
watch: {
num1(newVal,oldVal){
if(newVal > 3){
this.msg = "超出库存";
this.num1 = 3;
}else{
msg = "";
}
}
},
})
</script>
</body>
</html>