Skip to content

Commit 6d5261b

Browse files
author
ntepluhina
committed
fix: fixed computed doc
1 parent de0cdeb commit 6d5261b

File tree

1 file changed

+73
-85
lines changed

1 file changed

+73
-85
lines changed

src/guide/computed.md

Lines changed: 73 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ In-template expressions are very convenient, but they are meant for simple opera
1414

1515
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.
1616

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**.
1818

1919
### Basic Example
2020

@@ -26,26 +26,23 @@ That's why for any complex logic, you should use a **computed property**.
2626
```
2727

2828
```js
29-
const vm = 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-
return this.message
41-
.split('')
42-
.reverse()
43-
.join('')
44-
}
45-
}
29+
const vm = Vue.createApp({
30+
data() {
31+
return {
32+
message: "Hello"
33+
};
4634
},
47-
'#example'
48-
)
35+
computed: {
36+
// a computed getter
37+
reversedMessage() {
38+
// `this` points to the vm instance
39+
return this.message
40+
.split("")
41+
.reverse()
42+
.join("");
43+
}
44+
}
45+
}).mount("#example");
4946
```
5047

5148
Result:
@@ -55,9 +52,9 @@ Result:
5552
Here we have declared a computed property `reversedMessage`. The function we provided will be used as the getter function for the property `vm.reversedMessage`:
5653

5754
```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'
6158
```
6259

6360
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
106103
```
107104

108105
```js
109-
const vm = Vue.createApp().mount(
110-
{
111-
data() {
112-
return {
113-
firstName: 'Foo',
114-
lastName: 'Bar',
115-
fullName: 'Foo Bar'
116-
}
106+
const vm = 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;
117117
},
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;
125120
}
126-
},
127-
'#demo'
128-
)
121+
}
122+
}).mount("#demo");
129123
```
130124

131125
The above code is imperative and repetitive. Compare it with a computed property version:
132126

133127
```js
134-
const vm = Vue.createApp().mount(
135-
{
136-
data() {
137-
return {
138-
firstName: 'Foo',
139-
lastName: 'Bar'
140-
}
141-
},
142-
computed: {
143-
fullName() {
144-
return this.firstName + ' ' + this.lastName
145-
}
146-
}
128+
const vm = Vue.createApp({
129+
data() {
130+
return {
131+
firstName: "Foo",
132+
lastName: "Bar"
133+
};
147134
},
148-
'#demo'
149-
)
135+
computed: {
136+
fullName() {
137+
return this.firstName + " " + this.lastName;
138+
}
139+
}
140+
}).mount("#demo");
150141
```
151142

152143
Much better, isn't it?
@@ -199,38 +190,35 @@ For example:
199190
<!-- gives you the freedom to use what you're familiar with. -->
200191
<script src="https://cdn.jsdelivr.net/npm/axios@0.12.0/dist/axios.min.js"></script>
201192
<script>
202-
const watchExampleVM = Vue.createApp().mount(
203-
{
204-
data() {
205-
return {
206-
question: '',
207-
answer: 'Questions usually contain a question mark. ;-)'
208-
}
209-
},
210-
watch: {
211-
// whenever question changes, this function will run
212-
question(newQuestion, oldQuestion) {
213-
if (newQuestion.indexOf('?') > -1) {
214-
this.getAnswer()
215-
}
216-
}
217-
},
218-
methods: {
219-
getAnswer() {
220-
this.answer = 'Thinking...'
221-
axios
222-
.get('https://yesno.wtf/api')
223-
.then(response => {
224-
this.answer = _.capitalize(response.data.answer)
225-
})
226-
.catch(error => {
227-
this.answer = 'Error! Could not reach the API. ' + error
228-
})
193+
const watchExampleVM = Vue.createApp({
194+
data() {
195+
return {
196+
question: "",
197+
answer: "Questions usually contain a question mark. ;-)"
198+
};
199+
},
200+
watch: {
201+
// whenever question changes, this function will run
202+
question(newQuestion, oldQuestion) {
203+
if (newQuestion.indexOf("?") > -1) {
204+
this.getAnswer();
229205
}
230206
}
231207
},
232-
'#watch-example'
233-
)
208+
methods: {
209+
getAnswer() {
210+
this.answer = "Thinking...";
211+
axios
212+
.get("https://yesno.wtf/api")
213+
.then(response => {
214+
this.answer = _.capitalize(response.data.answer);
215+
})
216+
.catch(error => {
217+
this.answer = "Error! Could not reach the API. " + error;
218+
});
219+
}
220+
}
221+
}).mount("#watch-example");
234222
</script>
235223
```
236224

0 commit comments

Comments
 (0)