Computed偵聽多個數(shù)據(jù)變化,Watch只能偵聽data里單個數(shù)據(jù)變化
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>數(shù)據(jù)偵聽</title>
<script src="vue.js"></script>
</head>
<body>
<!-- 監(jiān)控購物車數(shù)據(jù)變化 -->
<div id="app">
<input type="number" v-model="price" />
<input type="number" v-model="number" />
<p>{{'總計:'+total}}</p>
</div>
<!-- computed偵聽多個數(shù)據(jù)變化,watch只能偵聽data里單個數(shù)據(jù)變化 -->
<script>
var vm = new Vue({
el:'#app',
data:{
price:18,
number:6
},
computed:{
total:function(){
return this.price*this.number
}
},
watch:{
price:function(val,oldval){
console.log(val)
}
}
})
</script>
</body>
</html>