I don't think that it's a good idea.
Put the condition inside the onMounted() function and declare the bar property outide the onMounted() function and keep it null if the condition is false.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Is it possible to wrap declarations in script setup with if and else? Here's an example
<script setup>
import { ref, onMounted } from 'vue'
const props = defineProps({
foo: {
type: Boolean,
default: false
}
});
if (props.foo) {
const bar = ref(null);
onMounted(() => {
// do something...
});
}
</script>
Please or to participate in this conversation.