computed:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
<div id="app">
<input type="text" v-model="first" /><br />
<input type="text" v-model="last" /><br />
<p>全名:{{full}}</p>
</div>
</body>
<script>
const app = new Vue({
el: "#app",
data: {
first: "张",
last: "三",
},
computed: {
//完整写法
full: {
get() {
return this.first + this.last;
},
set() {},
},
//简写
full() {
return this.first + this.last;
},
},
});
</script>
</html>
watch:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
<div id="app">
{{number.a}}
<button @click="qie">点击</button>
</div>
</body>
<script>
const app = new Vue({
el: "#app",
data: {
number: {
a: 1,
b: 2,
},
},
methods: {
qie() {
this.number.a++;
},
},
watch: {
// 完整写法
number: {
// immediate: true, //初始化时立即执行
// deep: true, //深度监视
handler(xin, old) {
console.log(xin, old);
},
},
// 简写
number(xin, old) {
console.log(xin, old);
},
},
});
</script>
</html>