計算 ‘ 單價 x 數量 = 總價 ’
watch:就不寫了,沒意思
computed:
computed:{
allPrice:function(){
return this.price*this.count
}
}
computed:{
allPrice:{
//get只讀
get:function(){
return this.price*this.count
},
//當改變結果allPrice時我們也可以通過set屬性里的一些js邏輯來改變參數的值
set:function(newVal){
this.price = newVal/this.count
}
}
}
只要改變能影響allPrice值的參數,都會立即改變allPrice頁面的值,
watch的話需要監聽每一個可能會被改變的值,參數過多時需要寫很多同樣的計算方法,當然可以調用同一個,但沒必要