Skip to content

Commit dc6d449

Browse files
fix: fixed disabling an input
1 parent a650a11 commit dc6d449

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

src/guide/essentials/watchers.md

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export default {
1313
data() {
1414
return {
1515
question: '',
16-
answer: 'Questions usually contain a question mark. ;-)'
16+
answer: 'Questions usually contain a question mark. ;-)',
17+
loading: false
1718
}
1819
},
1920
watch: {
@@ -26,16 +27,15 @@ export default {
2627
},
2728
methods: {
2829
async getAnswer() {
29-
this.$refs.input.disabled = true
30+
this.loading = true
3031
this.answer = 'Thinking...'
3132
try {
3233
const res = await fetch('https://yesno.wtf/api')
3334
this.answer = (await res.json()).answer
3435
} catch (error) {
3536
this.answer = 'Error! Could not reach the API. ' + error
3637
} finally {
37-
this.$refs.input.disabled = false
38-
this.$refs.input.focus()
38+
this.loading = false
3939
}
4040
}
4141
}
@@ -45,12 +45,12 @@ export default {
4545
```vue-html
4646
<p>
4747
Ask a yes/no question:
48-
<input ref="input" v-model="question" />
48+
<input v-model="question" :disabled="loading" />
4949
</p>
5050
<p>{{ answer }}</p>
5151
```
5252

53-
[Try it in the Playground](https://play.vuejs.org/#eNp9VE2Pm0AM/SvuqBJEzcJhb2m2VbrKYXtot+0euUzBhNkMM3Q+QqIo/71mCJD9lJBg7GeP37PNka2aJtl5ZAu2tLkRjfuSKdw32jgosOReOjhmCqDgjsez/hvAoPNGDSeAfx6tE1otIIrmg5Er26Ih06+z14K3nkt5gFwrx4UCPkZCzc02gc9Xs6iPP3WvU0jWcpdXi+G2NIW2QoU7NFN0XnG1QTsHVwkLpVd5MLdCSjBe9ZEDOlbYDjXNQctiOIz8AEQJl7BEqFz6Am0cfY1mFzgINyYbdKtAN54NnkDgCY8aXaULOzLh9qByuAid0oakHw2Wlm5uvEsKYflfiQXcgDMen+B6ockTPVRCbYXaJElylpEg5nBZLklvHTXQEp63XDgokeSNo8q5xi7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z/bY2TL2SH1Ec8CMTZjjhqaxh7e/v+ORvieQqvaSvN8Bf6HV0veSdG5fvSoo7Su/kO1D3f13SKInuz06VHYsahzzfl0yRj+s+3dKn9O9TW7HPrPLP624lFU=)
5454

5555
The `watch` option also supports a dot-delimited path as the key:
5656

@@ -77,21 +77,20 @@ import { ref, watch } from 'vue'
7777
7878
const question = ref('')
7979
const answer = ref('Questions usually contain a question mark. ;-)')
80-
const inputRef = ref()
80+
const loading = ref(false)
8181
8282
// watch works directly on a ref
8383
watch(question, async (newQuestion, oldQuestion) => {
8484
if (newQuestion.includes('?')) {
85-
inputRef.value.disabled = true
85+
loading.value = true
8686
answer.value = 'Thinking...'
8787
try {
8888
const res = await fetch('https://yesno.wtf/api')
8989
answer.value = (await res.json()).answer
9090
} catch (error) {
9191
answer.value = 'Error! Could not reach the API. ' + error
9292
} finally {
93-
inputRef.value.disabled = false
94-
inputRef.value.focus()
93+
loading.value = false
9594
}
9695
}
9796
})
@@ -100,13 +99,13 @@ watch(question, async (newQuestion, oldQuestion) => {
10099
<template>
101100
<p>
102101
Ask a yes/no question:
103-
<input ref="inputRef" v-model="question" />
102+
<input v-model="question" :disabled="loading" />
104103
</p>
105104
<p>{{ answer }}</p>
106105
</template>
107106
```
108107

109-
[Try it in the Playground](https://play.vuejs.org/#eNp9VE1z0zAQ/SuLLraH1D70FtIypdNDOUApPeoi7HWixpGMPuJkMv7vrKzIBQq92Nbu26fdp2ed2E3fl3uPbMlWtjayd2DR+f6aK7nrtXFwAoPtAgbh6g2M0Bq9g4wqMq64qrWyDqTqvXvEFq4CNi9S/KdH66RW53iWzRmh7IAmxb+dcRa89aLrjkAoJ6QC8cKxE2ZbwoeLIrBwVVXnlgZtthYaabB2VElIEVi5mtJ5ql+AsEdVQ65wSPstQHdNWhRwdQ0nrgBk+weqlKrufIM2zz5mRRExhDoPXe5F57FspBU/OmxoJmc8RkwcMyIokT1tpNpKtS7LkuQLCGeOiRDC1KSNQUtYMQjpoMUwQ7Zxr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j/DDjy94oOcZpK57Rew3wO/ojOpjJIH2qdcN2f6DN7l9nC47RfTsHg4etUtNpZUeJz5ndPPv32j9Yve6vE6DZuNvu1R2Tg==)
110109

111110
### Watch Source Types {#watch-source-types}
112111

@@ -262,9 +261,13 @@ The initial execution of the handler function will happen just before the `creat
262261
We can force a watcher's callback to be executed immediately by passing the `immediate: true` option:
263262

264263
```js
265-
watch(source, (newValue, oldValue) => {
266-
// executed immediately, then again when `source` changes
267-
}, { immediate: true })
264+
watch(
265+
source,
266+
(newValue, oldValue) => {
267+
// executed immediately, then again when `source` changes
268+
},
269+
{ immediate: true }
270+
)
268271
```
269272

270273
</div>
@@ -279,12 +282,16 @@ It is common for the watcher callback to use exactly the same reactive state as
279282
const todoId = ref(1)
280283
const data = ref(null)
281284

282-
watch(todoId, async () => {
283-
const response = await fetch(
284-
`https://jsonplaceholder.typicode.com/todos/${todoId.value}`
285-
)
286-
data.value = await response.json()
287-
}, { immediate: true })
285+
watch(
286+
todoId,
287+
async () => {
288+
const response = await fetch(
289+
`https://jsonplaceholder.typicode.com/todos/${todoId.value}`
290+
)
291+
data.value = await response.json()
292+
},
293+
{ immediate: true }
294+
)
288295
```
289296

290297
In particular, notice how the watcher uses `todoId` twice, once as the source and then again inside the callback.

0 commit comments

Comments
 (0)