You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/guide/computed.md
+73-85Lines changed: 73 additions & 85 deletions
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ In-template expressions are very convenient, but they are meant for simple opera
14
14
15
15
At this point, the template is no longer simple and declarative. You have to look at it for a second before realizing that it displays `message` in reverse. The problem is made worse when you want to include the reversed message in your template more than once.
16
16
17
-
That's why for any complex logic, you should use a **computed property**.
17
+
That's why for complex logic that includes reactive data, you should use a **computed property**.
18
18
19
19
### Basic Example
20
20
@@ -26,26 +26,23 @@ That's why for any complex logic, you should use a **computed property**.
26
26
```
27
27
28
28
```js
29
-
constvm=Vue.createApp().mount(
30
-
{
31
-
data() {
32
-
return {
33
-
message:'Hello'
34
-
}
35
-
},
36
-
computed: {
37
-
// a computed getter
38
-
reversedMessage() {
39
-
// `this` points to the vm instance
40
-
returnthis.message
41
-
.split('')
42
-
.reverse()
43
-
.join('')
44
-
}
45
-
}
29
+
constvm=Vue.createApp({
30
+
data() {
31
+
return {
32
+
message:"Hello"
33
+
};
46
34
},
47
-
'#example'
48
-
)
35
+
computed: {
36
+
// a computed getter
37
+
reversedMessage() {
38
+
// `this` points to the vm instance
39
+
returnthis.message
40
+
.split("")
41
+
.reverse()
42
+
.join("");
43
+
}
44
+
}
45
+
}).mount("#example");
49
46
```
50
47
51
48
Result:
@@ -55,9 +52,9 @@ Result:
55
52
Here we have declared a computed property `reversedMessage`. The function we provided will be used as the getter function for the property `vm.reversedMessage`:
56
53
57
54
```js
58
-
console.log(vm.reversedMessage) // => 'olleH'
59
-
vm.message='Goodbye'
60
-
console.log(vm.reversedMessage) // => 'eybdooG'
55
+
console.log(vm.reversedMessage);// => 'olleH'
56
+
vm.message="Goodbye";
57
+
console.log(vm.reversedMessage);// => 'eybdooG'
61
58
```
62
59
63
60
You can open the sandbox(TODO) and play with the example vm yourself. The value of `vm.reversedMessage` is always dependent on the value of `vm.message`.
@@ -106,47 +103,41 @@ Vue does provide a more generic way to observe and react to data changes on a Vu
106
103
```
107
104
108
105
```js
109
-
constvm=Vue.createApp().mount(
110
-
{
111
-
data() {
112
-
return {
113
-
firstName:'Foo',
114
-
lastName:'Bar',
115
-
fullName:'Foo Bar'
116
-
}
106
+
constvm=Vue.createApp({
107
+
data() {
108
+
return {
109
+
firstName:"Foo",
110
+
lastName:"Bar",
111
+
fullName:"Foo Bar"
112
+
};
113
+
},
114
+
watch: {
115
+
firstName(val) {
116
+
this.fullName= val +""+this.lastName;
117
117
},
118
-
watch: {
119
-
firstName(val) {
120
-
this.fullName= val +''+this.lastName
121
-
},
122
-
lastName(val) {
123
-
this.fullName=this.firstName+''+ val
124
-
}
118
+
lastName(val) {
119
+
this.fullName=this.firstName+""+ val;
125
120
}
126
-
},
127
-
'#demo'
128
-
)
121
+
}
122
+
}).mount("#demo");
129
123
```
130
124
131
125
The above code is imperative and repetitive. Compare it with a computed property version:
132
126
133
127
```js
134
-
constvm=Vue.createApp().mount(
135
-
{
136
-
data() {
137
-
return {
138
-
firstName:'Foo',
139
-
lastName:'Bar'
140
-
}
141
-
},
142
-
computed: {
143
-
fullName() {
144
-
returnthis.firstName+''+this.lastName
145
-
}
146
-
}
128
+
constvm=Vue.createApp({
129
+
data() {
130
+
return {
131
+
firstName:"Foo",
132
+
lastName:"Bar"
133
+
};
147
134
},
148
-
'#demo'
149
-
)
135
+
computed: {
136
+
fullName() {
137
+
returnthis.firstName+""+this.lastName;
138
+
}
139
+
}
140
+
}).mount("#demo");
150
141
```
151
142
152
143
Much better, isn't it?
@@ -199,38 +190,35 @@ For example:
199
190
<!-- gives you the freedom to use what you're familiar with. -->
0 commit comments