-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex9.html
64 lines (63 loc) · 2.66 KB
/
index9.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
<!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/dist/vue.js"></script>
</head>
<body>
<div id="app">
<p>{{msg}}</p>
<button>{{getMsg}}</button>
<input type="text" name="" id="" v-model="msg" />
<h3>{{getMsgStr}}</h3>
<h3>computed: ---{{getMsg}}</h3>
<h3>computed: ---{{getMsg}}</h3>
<h3>computed: ---{{getMsg}}</h3>
<h3>reverseMsg: Method---{{reverseMessage()}}</h3>
<h3>reverseMsg: Method---{{reverseMessage()}}</h3>
<h3>reverseMsg: Method---{{reverseMessage()}}</h3>
</div>
</body>
<script>
var vm = new Vue({
el: "#app",
data() {
return {
msg: "hello",
val: "",
};
},
methods: {
reverseMessage() {
console.log("methods");
return this.msg.split("").reverse().join("");
},
},
computed: {
getMsg: {
get() {
console.log("touch get to getmsg");
return this.msg.split("").reverse().join("");
},
set(value) {
console.log("trigger set !");
console.log("set", value);
this.val = value;
},
},
getMsgStr() {
console.log("MsgStr");
return this.msg.split("").reverse().join("");
},
},
});
// computed:计算属性通过计算得出的属性//计算属性在打开页面的时候会自动执行一次
// 1.computed计算属性依赖于data中的数据,会时刻监听依赖的data中的数据,只要依赖的数据发生变化, computed的值也会发生变化
// 2.computed是有缓存的,只有当数据发生变化的时候才会重新计算,否则从缓存中直接读取数据
// 3.computed 中的get是获取当前属性的值, set是当设置当前属性值的时候触发.大部分情况下,我们只是想获取当前属性的值,很少去设置
// 当前属性的值,所以可以简写成函数的形式(默认获取的是get属性).如果我们需要给当前属性设置值,是不能简写的
</script>
</html>