N
Nuxt2mo ago
ManUtopiK

How to check if a composable exists ?

I want to check if a composable exists in a component. With import { myComposable } from '#imports', importing the composable throw an error and break nuxt if the composable doesn't exist.
1 Reply
Dragofafmir
Dragofafmir2mo ago
Did you try to try catch an const { myComposable } = await import('./path/to/myComposable'), try to use it or catch it ? Something like this
<script setup lang="ts">
const composableLoaded = ref(false)

const loadMyComposable = async () => {
try {
const { myComposable } = await import('./path/to/myComposable')
myComposable()
composableLoaded.value = true
} catch (error) {
console.error("...", error)
composableLoaded.value = false
}
}

onMounted(() => {
loadMyComposable()
})
</script>
<script setup lang="ts">
const composableLoaded = ref(false)

const loadMyComposable = async () => {
try {
const { myComposable } = await import('./path/to/myComposable')
myComposable()
composableLoaded.value = true
} catch (error) {
console.error("...", error)
composableLoaded.value = false
}
}

onMounted(() => {
loadMyComposable()
})
</script>