admin管理员组文章数量:1403352
What works:
I want to render an element only if some boolean value is true using Vue 3. This is an example following the Vue 3 documentation and it works just fine:
Template:
...
<div id="btnReady" @click="ready = !ready">
<img v-if="!ready" src="ready.svg" alt="" />
<img v-else src="unready.svg" alt="" />
</div>
...
Script:
<script setup>
import { ref } from "vue";
let ready = ref(false);
</script>
What I do not understand:
That makes perfect sense to me, except i don't really understand the need of ref()
. I read the documentation about it and didn't really understand how/why vue requires the initialization like this let ready = ref(false)
and why using let ready = false
won't react to changes to the ready
variable.
What does NOT work:
Beside toggling a boolean value, the onclick
handler should also perform some other actions. So instead of toggling the value like this onclick="ready = !ready"
i want to call a function onclick="toggleReady"
. The value of ready
will be toggled within the function and some other actions will be executed.
The function is executed on click just fine but Vue does NOT react to the changes of the ready
variable and the v-if
statement does not "work" properly -> the element is still visible.
Template:
<div id="btnReady" @click="toggleReady">
<img v-if="!isReady" src="ready.svg" alt="" />
<img v-else src="unready.svg" alt="" />
</div>
Script:
<script setup>
import { ref } from "vue";
let ready = ref(false);
let toggleReady = () => {
ready = !ready;
// do some more stuff
};
</script>
Can someone explain me this behaviour and how I can fix it?
PS: I also tried using a puted
property in the v-if
statement instead of a normal variable. Didn't work...
What works:
I want to render an element only if some boolean value is true using Vue 3. This is an example following the Vue 3 documentation and it works just fine:
Template:
...
<div id="btnReady" @click="ready = !ready">
<img v-if="!ready" src="ready.svg" alt="" />
<img v-else src="unready.svg" alt="" />
</div>
...
Script:
<script setup>
import { ref } from "vue";
let ready = ref(false);
</script>
What I do not understand:
That makes perfect sense to me, except i don't really understand the need of ref()
. I read the documentation about it and didn't really understand how/why vue requires the initialization like this let ready = ref(false)
and why using let ready = false
won't react to changes to the ready
variable.
What does NOT work:
Beside toggling a boolean value, the onclick
handler should also perform some other actions. So instead of toggling the value like this onclick="ready = !ready"
i want to call a function onclick="toggleReady"
. The value of ready
will be toggled within the function and some other actions will be executed.
The function is executed on click just fine but Vue does NOT react to the changes of the ready
variable and the v-if
statement does not "work" properly -> the element is still visible.
Template:
<div id="btnReady" @click="toggleReady">
<img v-if="!isReady" src="ready.svg" alt="" />
<img v-else src="unready.svg" alt="" />
</div>
Script:
<script setup>
import { ref } from "vue";
let ready = ref(false);
let toggleReady = () => {
ready = !ready;
// do some more stuff
};
</script>
Can someone explain me this behaviour and how I can fix it?
PS: I also tried using a puted
property in the v-if
statement instead of a normal variable. Didn't work...
-
1
ready.value = !ready.value;
as per documentation – Bravo Commented Mar 21, 2022 at 8:26 -
note: if you're using
vite
- you can try the experimental Reactivity Transform by adding{reactivityTransform: true}
firvue
in vite.config.js ... i.e.plugins: [ vue({reactivityTransform: true}) ]
- which would allow you to drop the.value
- but it is experimental – Bravo Commented Mar 21, 2022 at 8:33 -
This works indeed. But why can we simply write
onclick="ready = !ready"
but have to writeready.value = !ready.value
when using a function? Also why do i have to access thevalue
property in the script section but when checking in thev-if
statement i check like thisv-if="ready"
and notv-if="ready.value"
? – Quaenor Commented Mar 21, 2022 at 8:37 - EDIT: I should have read the docs before asking questions... Makes sense to me, although i think the aproach in Vue 2 was more straight forward to understand. Not sure how exactly this was done there tho. – Quaenor Commented Mar 21, 2022 at 8:43
-
how exactly this was done there
how what was done where? the first code doesn't need to use.value
... as documented – Bravo Commented Mar 21, 2022 at 10:12
1 Answer
Reset to default 5As @Bravo mentioned in the ments you should use in your js code ready.value = !ready.value;
instead of ready = !ready;
(https://vuejs/api/reactivity-core.html#ref)
If you just want to change the source of an image, you can use conditional attribute rendering like so:
...
<div id="btnReady" @click="ready = !ready">
<img :src="!ready ? 'ready.svg' : 'unready.svg'" alt="" />
</div>
...
本文标签: javascriptVue 3 vif not reacting to changes within a functionStack Overflow
版权声明:本文标题:javascript - Vue 3 v-if not reacting to changes within a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744376252a2603274.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论