Skip to content

Commit 081bd6a

Browse files
author
ntepluhina
committed
Fixed computed example
1 parent 60b0d44 commit 081bd6a

File tree

3 files changed

+31
-43
lines changed

3 files changed

+31
-43
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"build": "vuepress build src"
88
},
99
"dependencies": {
10-
"axios": "^0.19.1",
11-
"lodash": "^4.17.15"
10+
"axios": "^0.19.1"
1211
}
1312
}

src/.vuepress/components/computed-2.vue

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,24 @@ export default {
1515
data() {
1616
return {
1717
question: '',
18-
answer: 'I cannot give you an answer until you ask a question!'
18+
answer: 'Questions usually contain a question mark. ;-)'
1919
}
2020
},
2121
watch: {
22+
// whenever question changes, this function will run
2223
question(newQuestion, oldQuestion) {
23-
this.answer = 'Waiting for you to stop typing...'
24-
this.debouncedGetAnswer()
24+
if (newQuestion.indexOf('?') > -1) {
25+
this.getAnswer()
26+
}
2527
}
2628
},
27-
created() {
28-
this.debouncedGetAnswer = debounce(this.getAnswer, 500)
29-
},
3029
methods: {
3130
getAnswer() {
32-
if (this.question.indexOf('?') === -1) {
33-
this.answer = 'Questions usually contain a question mark. ;-)'
34-
return
35-
}
3631
this.answer = 'Thinking...'
3732
axios
3833
.get('https://yesno.wtf/api')
3934
.then(response => {
40-
this.answer = capitalize(response.data.answer)
35+
this.answer = _.capitalize(response.data.answer)
4136
})
4237
.catch(error => {
4338
this.answer = 'Error! Could not reach the API. ' + error

src/guide/computed.md

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ That's why for any complex logic, you should use a **computed property**.
2828
```js
2929
const vm = Vue.createApp().mount(
3030
{
31-
data: {
32-
message: 'Hello'
31+
data() {
32+
return {
33+
message: 'Hello'
34+
}
3335
},
3436
computed: {
3537
// a computed getter
@@ -106,10 +108,12 @@ Vue does provide a more generic way to observe and react to data changes on a Vu
106108
```js
107109
const vm = Vue.createApp().mount(
108110
{
109-
data: {
110-
firstName: 'Foo',
111-
lastName: 'Bar',
112-
fullName: 'Foo Bar'
111+
data() {
112+
return {
113+
firstName: 'Foo',
114+
lastName: 'Bar',
115+
fullName: 'Foo Bar'
116+
}
113117
},
114118
watch: {
115119
firstName(val) {
@@ -129,9 +133,11 @@ The above code is imperative and repetitive. Compare it with a computed property
129133
```js
130134
const vm = Vue.createApp().mount(
131135
{
132-
data: {
133-
firstName: 'Foo',
134-
lastName: 'Bar'
136+
data() {
137+
return {
138+
firstName: 'Foo',
139+
lastName: 'Bar'
140+
}
135141
},
136142
computed: {
137143
fullName() {
@@ -192,37 +198,25 @@ For example:
192198
<!-- is able to remain small by not reinventing them. This also -->
193199
<!-- gives you the freedom to use what you're familiar with. -->
194200
<script src="https://cdn.jsdelivr.net/npm/axios@0.12.0/dist/axios.min.js"></script>
195-
<script src="https://cdn.jsdelivr.net/npm/lodash@4.13.1/lodash.min.js"></script>
196201
<script>
197202
const watchExampleVM = Vue.createApp().mount(
198203
{
199-
data: {
200-
question: '',
201-
answer: 'I cannot give you an answer until you ask a question!'
204+
data() {
205+
return {
206+
question: '',
207+
answer: 'Questions usually contain a question mark. ;-)'
208+
}
202209
},
203210
watch: {
204211
// whenever question changes, this function will run
205212
question(newQuestion, oldQuestion) {
206-
this.answer = 'Waiting for you to stop typing...'
207-
this.debouncedGetAnswer()
213+
if (newQuestion.indexOf('?') > -1) {
214+
this.getAnswer()
215+
}
208216
}
209217
},
210-
created() {
211-
// _.debounce is a function provided by lodash to limit how
212-
// often a particularly expensive operation can be run.
213-
// In this case, we want to limit how often we access
214-
// yesno.wtf/api, waiting until the user has completely
215-
// finished typing before making the ajax request. To learn
216-
// more about the _.debounce function (and its cousin
217-
// _.throttle), visit: https://lodash.com/docs#debounce
218-
this.debouncedGetAnswer = _.debounce(this.getAnswer, 500)
219-
},
220218
methods: {
221219
getAnswer() {
222-
if (this.question.indexOf('?') === -1) {
223-
this.answer = 'Questions usually contain a question mark. ;-)'
224-
return
225-
}
226220
this.answer = 'Thinking...'
227221
axios
228222
.get('https://yesno.wtf/api')
@@ -244,6 +238,6 @@ Result:
244238

245239
<computed-2 />
246240

247-
In this case, using the `watch` option allows us to perform an asynchronous operation (accessing an API), limit how often we perform that operation, and set intermediary states until we get a final answer. None of that would be possible with a computed property.
241+
In this case, using the `watch` option allows us to perform an asynchronous operation (accessing an API) and sets a condition for performing this operation. None of that would be possible with a computed property.
248242

249243
In addition to the `watch` option, you can also use the imperative [vm.\$watch API](TODO:../api/#vm-watch).

0 commit comments

Comments
 (0)