Dynamic v-model with component :is
Hello i have this use case in my mind.
Let's assume i have a list of components like this ( For context this is a modal with customer info reviews, product bought etc)
And use component :is like this
What i have in mind is if it would be possible to make the v-model key dynamic depending on the component for example after refactoring the componentMap
Atm i have all the v-models which it works but idk if it is the best approach
2 Replies
Hi @Wicker. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
To implement dynamic v-model bindings with the
:is
directive in Nuxt 3, you can use a combination of computed properties and the v-bind
directive. Here's how you can approach this:
1. First, define your componentMap
with the structure you proposed:
2. Create a computed property that returns the current component's configuration:
3. Use the v-bind
directive with an object to dynamically set the v-model:
This approach allows you to dynamically set the v-model key based on the current component's configuration.
However, it's important to note that using resolveComponent
with variables is not supported in Nuxt 3. As mentioned in the Nuxt documentation (https://nuxt.com/docs/guide/directory-structure/components#dynamic-components), you should use one of the following approaches:
For your specific case, you might want to adjust your code to:
This approach should work well with Nuxt 3's component resolution system while still allowing you to dynamically set v-model bindings based on the component being rendered.
Stack Overflow: Nuxt3 dynamic <component> (https://stackoverflow.com/questions/78319986)
Nuxt Documentation: Dynamic Components (https://nuxt.com/docs/guide/directory-structure/components#dynamic-components)
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@142400234444685312> Kapa.ai is still learning and improving, please let me know how I did by reacting below.