<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>test</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="root">
<div>计算属性:{{total}}</div>
<div>watch监听:{{newTotal}}</div>
</div>
</body>
<script>
const app = Vue.createApp({
data() {
return {
count: 2,
price: 5,
}
},
watch: {
price(newPrice, oldPrice) {
this.newTotal = newPrice * this.count
}
},
computed: {
total() {
return this.price * this.count
}
},
methods: {
},
});
const vm = app.mount('#root');
</script>
</html>
现在我们改下price的值
结论:
- 只有在监听到对应的变量改变时,watch才会执行;