文章來源:http://www.zghlxwxcb.cn/news/detail-660515.html
<!DOCTYPE html>
<html lang="en">
<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>Document</title>
</head>
<body>
<div id="app">
<!-- 某些結果是基于之前數(shù)據(jù)實時計算出來的,我們可以利用計算屬性。來完成 -->
<ul>
<li>西游記; 價格:{{xyjPrice}},數(shù)量:<input type="number" v-model="xyjNum"> </li>
<li>水滸傳; 價格:{{shzPrice}},數(shù)量:<input type="number" v-model="shzNum"> </li>
<li>總價:{{totalPrice}}</li>
{{msg}}
</ul>
</div>
<script src="../node_modules/vue/dist/vue.js"></script>
<script>
//watch可以讓我們監(jiān)控一個值的變化。從而做出相應的反應。
new Vue({
el: "#app",
data: {
xyjPrice: 99.98,
shzPrice: 98.00,
xyjNum: 1,
shzNum: 1,
msg: ""
},
computed: {
totalPrice(){
return this.xyjPrice*this.xyjNum + this.shzPrice*this.shzNum
}
},
watch: {
xyjNum(newVal,oldVal){
if(newVal>=3){
this.msg = "庫存超出限制";
this.xyjNum = 3
}else{
this.msg = "";
}
}
},
})
</script>
</body>
</html>
文章來源地址http://www.zghlxwxcb.cn/news/detail-660515.html
到了這里,關于GuLi商城-前端基礎Vue-計算屬性computed和偵聽器watch的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!