-
Notifications
You must be signed in to change notification settings - Fork 4
/
watcher.html
70 lines (65 loc) · 2.37 KB
/
watcher.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<!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">
<title>Computed Properties and Watchers</title>
<script src="../../lib/vue.js"></script>
</head>
<body>
<div id="app">
<!-- 1、计算属性 -->
<!-- 输入的值:<input type="text" v-model="message"><br /> -->
<!-- 反转的值:{{reversedMessage}}<br /> -->
<!-- 2、监听器 -->
用户姓名:<input type="text" v-model="User.name"><br />
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
message: '',
User: {
name: 'zhangsan',
gender: 'male'
}
},
computed: {
// reversedMessage: function () {
// //这里的 this 指向当前的 vm 实例
// return this.message.split('').reverse().join('')
// }
reversedMessage: {
get: function () {
return this.message.split('').reverse().join('')
},
set: function (value) {
this.message = value.split('').reverse().join('')
}
}
},
watch: {
//1、回调为未创建的方法
'message': function (newValue, oldValue) {
console.log(`新值:${newValue} --------- 旧值:${oldValue}`)
},
//2、回调为已创建好的方法
'message': 'recording',
//3、回调为对象
'User': {
handler: function (newValue, oldValue) {
console.log(`对象记录:新值:${newValue.name} --------- 旧值:${oldValue.name}`)
},
deep: false//未设置或设置为 false 时则无法监听到对象中属性的改变
}
},
methods: {
recording: function (newValue, oldValue) {
console.log(`method记录:新值:${newValue} --------- 旧值:${oldValue}`)
}
}
})
</script>
</body>
</html>