I created a composable with a reactive boolean. For some reason, the reactive boolean only works with .value
in the template v-if
. It doesn't automatically unwraps it inside the template.
But if I use a boolean ref directly on the vue component, it works. I reproduced it here
The code:
// useToggle.ts file
import { ref } from "vue";
const visible = ref(true);
export function useToggle() {
function toggle() {
visible.value = !visible.value;
}
function close() {
visible.value = false;
}
return {
visible,
toggle,
close,
};
}
// App.vue file
<script setup lang="ts">
import { ref } from "vue";
import { useToggle } from "./useToggle";
const visible = ref(true);
const t = useToggle();
</script>
<template>
<button @click="visible = !visible">toggle local</button>
<div v-if="visible">local</div>
<button @click="t.toggle">toggle global without value</button>
<div v-if="t.visible">global (this doesn't work)</div>
<div v-if="t.visible.value">global (this doesn't work)</div>
{{t.visible}} (but this, updates)
</template>
I don't understand why the v-if doesn't work the same. What can I do to make the composable work like the local ref?
t
– Wade