Vue 的观察属性($watch)

与computer属性类似,用于观察量的变化,然后进行相应的处理。(从Angular而来)

<!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">

<script src="https://unpkg.com/vue/dist/vue.js"></script>

<title>Document</title>

</head>

<body>

<div id = "myApp">

<p>任天堂发布的新主机价格是:{{price}}元,含税价格为{{priceInTax}}元,折合人名币为:{{priceChinaRMB}}元</p>

<button @click = "btnClick(10000)" >加价10000</button>

</div>

<script>

var myApp = new Vue({

el:"#myApp",

data:{

price:0,

priceInTax:0,

priceChinaRMB:0,

},

watch:{

price:function(newVal,oldVal){

console.log(newVal,oldVal);

this.priceInTax = Math.round(this.price * 1.08);

this.priceChinaRMB = Math.round(this.priceInTax / 16.75);

},

},

methods:{

btnClick:function(newPrice){

this.price += newPrice;

},

},

});

myApp.price = 29980;

</script>

</body>

</html>

猜你喜欢

转载自blog.csdn.net/xlh006/article/details/83592584