N
Nuxt4mo 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.
6 Replies
Dragofafmir
Dragofafmir4mo 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>
ManUtopiK
ManUtopiK2mo ago
I reply a long time ago. First, thank you for your response. It looks fine but can serve my purpose. In fact, I use nuxt layers. I want to check in a component of a layer if the composable exists. So, I can't rely on path/to/myComposable because the composable is outside the package. Using const { myComposable } = await import('#imports') break nuxt build if the composable doesn't exist. Any other idea to do that ?
handshake
handshake2mo ago
I'm just curious, but in what situation would this happen? If you're expecting to use a composable, didn't you create it already?
ManUtopiK
ManUtopiK2mo ago
I have a layer with some components shared between two apps. I want the behaviour of the components to be different on one app and use a composable if it presents. Another use case I have for this. I want to release a library and make it open source. This library contains several components with some text and I want to provide translation with i18n. How to check in my components if i18n composable is here and translate text ? Or provide default EN text if there's no i18n composable ? Of course, I don't want to ship i18n in my library.
Orbis
Orbis2mo ago
@ManUtopiK you can maybe use a Provider component to wrap your App and provide and inject your config
ManUtopiK
ManUtopiK2mo ago
Ah yes. Thanks for your tip. I will take a try...
Want results from more Discord servers?
Add your server
More Posts
V-Calendar's DatePicker in FormGroup (Nuxt UI) with Yup validationHi ! I tried for a while yesterday to use the DatePicker component of V-Calendar, highlighted in thCustom directive stops working after upgrading from Vue 3.4.21 to 3.4.22I have been having a really bad time with minor Vue version upgrades with my nuxt app. The most receIs it possible for any env variables to be leaked from the nitro server package?After build I can see my db connection info from the env variable embedded into the node-server.mjs Adding a third party script to Nuxt websiteI'm currently trying to follow this guide: https://www.cookieyes.com/documentation/cookie-banner-on-Fetch Data even on page reload using custom Fetch ComposableI am trying to fetch data in a page. I am using a custom Fetch composable. But the fetch seems only After upgrading, computed props are slow and throwing call stack size errUpgrading from `nuxt 3.8.1` `vue 3.3.8` `vite 4.5.0` -> `nuxt 3.11.2` `vue 3.4.24` `vite 5.2.10` MuseFetch triggers on ssr while server: false ?I put console.log in my useFetch, and I can see that getCachedData get triggered twice while on ssr.[Solved]Iterator is undefined in V-forHello! I have a drop down that's supposed to change based on the choice of a radio button. This workVercel Vs Cloudflareany opinions in regard why to choose one over the other ? as far as my comparison goes. i think theHover state only for table body rowsHi. I know that rows are highlighted in selectable mode. But in my case I just want highlight rows o